1 / 12

Lecture 20: C File Processing

Lecture 20: C File Processing. Why Using Files?. Storage of data in variables and arrays is temporary 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.

anika
Download Presentation

Lecture 20: C File Processing

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 20: C File Processing

  2. Why Using Files? • Storage of data in variables and arrays is temporary • 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. C Files and Streams • C views each file as a sequence of bytes • File ends with the end-of-filemarker • Stream created when a file is opened • Provide communication channel between files and programs • Opening a file returns a pointer to a FILE structure • Defined in <stdio.h> • Contains information used to process the file. • Three files and their associated streams are automatically opened when program execution begins. • The standard input -- stdin (keyboard) • The standard output -- stdout (keyboard) • The standard error -- stderr (screen)

  4. Opening a File for Sequential-Access • Opening a file for reading • Define a file pointer FILE *rfPtr; • Defines a FILE pointer called rfPtr; • Open the file rfPtr = fopen(“sData.txt”, “r”); • Function fopen returns a FILE pointer to the file specified; link it to the file to read • Takes two arguments • Filename -- file to open • File open mode -- “r” Open an existing file for reading. • If open fails, NULL returned

  5. Opening a File for Sequential-Access • Using fscanf to read data from the file fscanf(rfPtr, “%f”, buffer); • Like scanf, except first argument is a FILE pointer (pointer to the file you want to read from) • Data read from beginning to end • Checking the end of the file feof(rfPtr) • Returns true if end-of-file indicator (no more data to process) is set for the specified file. • Closing the file fclose(rfPtr); • Closes the specified file • Performed automatically when program ends • Good practice to close file explicitly

  6. Creating a Sequential-Access File • Creating a file for writing • Define a file pointer FILE *wfPtr; • Creates a FILE pointer called wfPtr; • Create the file wfPtr = fopen(“cData.txt”, “w”); • Function fopen returns a FILE pointer to the file specified • Takes two arguments • Filename -- file to open • File open mode -- “w” Create a file for writing. If the file already exists, discard the current contents. • If open fails, NULL returned

  7. Creating a Sequential-Access File • Writing data to the file fprintf(wfPtr, “%.3f ”, buffer); • Like printf, except first argument is a FILE pointer (pointer to the file you want to print in) • Closing the file fclose(wfPtr); • Closes the spedified file • Performed automatically when program ends • Good practice to close file explicitly

  8. File Opening Modes

  9. Other Issues Programs may process no files, one file, or many files. Each file must have a unique name and should have its own pointer.

  10. Formatting Output printf( ), sprintf( ), and fprintf( ).

  11. Formatting Output • printf( ), sprintf( ), and fprintf( ). • Field width • The exact size of a field in which data is printed • An integer representing the field width is inserted between the percent sign (%) and the conversion specifier. Ex. %6d • Field widths can be used with all conversion specifiers • Precision for floating-point numbers • The number of digits to appear after the decimal point. Ex. %6.2f • When printed with a precision smaller than the original number of decimal places in the value, the value is rounded.

  12. Formatting Input scanf( ), sscanf( ), and fscanf( ).

More Related