1 / 9

Lecture 17: C File Processing (FILE I/O or FILE INPUT/OUTPUT)

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

dolan
Download Presentation

Lecture 17: C File Processing (FILE I/O or FILE INPUT/OUTPUT)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 17: C File Processing (FILE I/O or FILE INPUT/OUTPUT)

  2. 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

  3. 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)

  4. 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);

  5. 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"); }

  6. 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

  7. 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

  8. 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

  9. 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!

More Related