Lecture 20 C File Processing Why Using Files

  • Slides: 12
Download presentation
Lecture 20: C File Processing

Lecture 20: C File Processing

Why Using Files? Storage of data in variables and arrays is temporary Data lost

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

C Files and Streams C views each file as a sequence of bytes File

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

Opening a File for Sequential-Access Opening a file for reading Define a file pointer

Opening a File for Sequential-Access Opening a file for reading Define a file pointer FILE *rf. Ptr; Defines a FILE pointer called rf. Ptr; Open the file rf. Ptr = fopen(“s. Data. 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

Opening a File for Sequential-Access Using fscanf to read data from the file fscanf(rf.

Opening a File for Sequential-Access Using fscanf to read data from the file fscanf(rf. Ptr, “%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(rf. Ptr) Returns true if end-of-file indicator (no more data to process) is set for the specified file. Closing the file fclose(rf. Ptr); Closes the specified file Performed automatically when program ends Good practice to close file explicitly

Creating a Sequential-Access File Creating a file for writing Define a file pointer FILE

Creating a Sequential-Access File Creating a file for writing Define a file pointer FILE *wf. Ptr; Creates a FILE pointer called wf. Ptr; Create the file wf. Ptr = fopen(“c. Data. 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

Creating a Sequential-Access File Writing data to the file fprintf(wf. Ptr, “%. 3 f

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

File Opening Modes

File Opening Modes

Other Issues Programs may process no files, one file, or many files. Each file

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.

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

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

Formatting Output printf( ), sprintf( ), and fprintf( ). Field width The exact size

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. %6 d 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. 2 f When printed with a precision smaller than the original number of decimal places in the value, the value is rounded.

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

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