130 likes | 255 Views
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.
E N D
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 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)
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
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
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
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
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( ). • 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.
Formatting Input scanf( ), sscanf( ), and fscanf( ).