90 likes | 313 Views
Lecture 17: C File Processing (FILE I/O or FILE INPUT/OUTPUT). Why Using Files?. Storage of data in variables and arrays is temporary (memory) Data lost when a program terminates . Files are used for permanent retention of data Stored on secondary storage devices Disks
E N D
Lecture 17: C File Processing (FILE I/O or FILE INPUT/OUTPUT)
Why Using Files? • Storage of data in variables and arrays is temporary (memory) • Data lost when a program terminates. • Files are used for permanent retention of data • Stored on secondary storage devices • Disks • Optical disks (CDs, DVDs) • USB memory key
Opening a File • Define a FILE pointer called rfPtr; • FILE *rfPtr; • The fopenfunction links a FILE pointer to the file to read: • rfPtr= fopen(“sData.txt”, “r”); • Or write: • rfPtr= fopen(“sData.txt”, “w”); • fopen Takes two arguments • Filename -- file to open (“sData.txt”) • File open mode -- “r” or “w” – there are others as well • If open fails, NULL returned (i.e. there is nothing there)
Reading a file and writing to a file • To read a file: • fscanf(rfPtr, “%f”, &variable); • Like scanf, except reads from file instead of screen • To write to a file • fprintf(rfPtr, “%f”, variable); • Like printf except prints to file instead of screen • Check to see if end of file is reached: feof(rfPtr) • Closing the file fclose(rfPtr);
Other Issues Each file must have a unique name and should have its own pointer. It is usually good practice to also check if the file can be opened by checking if the file is NULL (although the examples in the next slide do not do this) The best way to do this is the following: if((fileptr = fopen("numbers.txt","r"))==NULL) { printf("Could not open numbers.txt\n"); }
What does this code do? #include <stdio.h> int main(void) { FILE *fp; inti, n=10; fp = fopen("data1", "w"); for (i=0;i<n;i++) { fprintf(fp,“%d\n”, i+1); } fclose(fp); return 0; } 1. This prints to file data1 2. This prints to the screen 3. This prints to file fp 4. This reads from file data1
What does this code do? #include <stdio.h> int main(void) { FILE *fr; inti, n=10; fr = fopen("data1", “r"); while (!feof(fr)) { fscanf(fr,“%d”, &i); } fclose(fr); return 0; } 1. This prints to file data1 2. This prints to the screen 3. This prints to file fp 4. This reads from file data1
What does this code print out to output.dat when the contents of data1 is: 0 1 2 3 4 5 1. 1 2 3 4 5 6 6 #include <stdio.h> int main(void) { FILE *fr, *fp; inti=0; fr = fopen("data1", “r"); fp= fopen(“output.dat”, “w”); fscanf(fr, “%d”, &i); while (!feof(fr)) { fprintf(fp,“%d\n”, i+1); fscanf(fr,“%d”, &i); } fclose(fr); fclose(fp); return 0; } 2. 1 2 3 4 5 6 4. 0 1 2 3 4 5 5 3. 0 1 2 3 4 5
What does this code print out to output.dat when the contents of data1 is: 0 1 2 3 4 5 1. 1 2 3 4 5 6 6 2. 1 2 3 4 5 6 #include <stdio.h> int main(void) { FILE *fr, *fp; inti, n=10; fr = fopen("data1", “r"); fp = fopen(“output.dat”, “w”); while (!feof(fr)) { fscanf(fr,“%d”, &i); fprintf(fp,“%d\n”, i+1); } fclose(fr); fclose(fp); return 0; } 4. 0 1 2 3 4 5 5 3. 0 1 2 3 4 5 Be careful, the feof returns as true only when it tries to scan in a line that isn’t there! So, it could end up printing the last line twice!