vi I have a function that will read a file of numbers and place them into an arr
ID: 3620313 • Letter: V
Question
viI have a function that will read a file of numbers and place them into an array. I need to create a new function that will, given that stored array, output the array to a new file.
This is the code I have:
int Save_File(char *Filename, long *Array, int Size) {
FILE *fp = fopen(Filename, "wb");
if (fp == NULL) { // check to see if file opens; if NULL, failed
printf("Error opening file. ");
return 0;
}
if (fwrite(&Array, sizeof(int), Size, fp) != Size) {
printf("File write error.");
}
fclose(fp);
return (Size);
}
--
Right now, it does not work. When I check the output file, there is one line that displays "^B^Zˆ^B^WF
Explanation / Answer
The problem was, you were trying to output longs to the file, and expected to read them as characters. This should do it for you: int Save_File(char *Filename, long *Array, int Size) { if (fp == NULL) { // check to see if file opens; if NULL, failed } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.