Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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-file marker 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 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( ).


Download ppt "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."

Similar presentations


Ads by Google