160 likes | 420 Views
C File Processing. Objectives. To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar with random-access file processing. Date Hierarchy. Tom Green. Judy Smith. File. Stan Miller.
E N D
Objectives • To be able to create, write and update files. • To become familiar with sequential access file processing. • To become familiar with random-access file processing.
Date Hierarchy Tom Green Judy Smith File Stan Miller Randy Walter Record Randy Walter Randy Field 01010010 Byte (ASCII Character R) 1 Bit
C’s View of a File of n-bytes 0 1 2 3 4 5 n-1 eof Each file will be accompanied with three streams when being executed. They are: stadin, stdout and stderr. Standard library then provides many functions for reading data from files and writing data to files. Such as: fgets read one char from a file. ( fgetc(stdin) then get from the Standard input=getchar ( ) ) Similarly, fscanf and fprintf will handle file input and output.
Creating a Sequential File • C impose no structure on a file. • Thus, notions like a record of a file do not exist as part of the C language. • The programmer must provide any file structure to meet the requirements of each particular application. • Example: week10 cfile.c
File Open Modes • Mode Description • r open a file for reading w open a file for writing a Append r+ open a file for update (r & w) w+ Create a file for update a+ Append; open or create a file for update; writing is done at the end of the file. ** w and w+ will discard the existing file.
Open an Existing File • Example: /week012/creat_sfile.c • fscanf( cfPtr, “%d%s%lf”, &account, name, &balance);
More Example • Credit inquiry program. • week012 credit.c • rewind ( cfPtr); • To move the file pointer to the beginning of the specified file.
Random-Access Files • Normally, they have fixed –length records for individual records. • May be accessed directly (and thus quickly) without searching through other records. • Main applications: • Airline reservation system, banking system, point-of-sale system and other kinds of transaction processing systems that require rapid access to specific data.
C’s view of a random-access file 300 400 100 200 0 Byte offset Each record has 100 bytes
Functions Used in Such Files • 1. fwrite – transfers a specified number of bytes beginning at a specified location in memory to a file. • fwrite ( &number, sizeof( int ), 1, fPtr ) • Will write 4-bytes from variable number to the file represented by fPtr. • Where the third parameter is the number of elements needs to write; it can be 1 int or 1 structure a time. • Example: week012/creat_rfile.c • 2. fread – is similar to fwrite
Example • Create a credit processing system capable of storing up to 100 fixed-length records. Each record should consists of an account number that will be used as the record key, a last name, a first name and a balance. • The first program—create the random file. • create_rfile.c
Program Remark • The statement of fwrite( &blankClient, sizeof(struct clientsData), 1, cfPtr ); Causes the structure blankClient of sizeof( struct clientsData ) to be written to the file pointed by cfPtr. *** sizeof is a compile-time unary operator that returns an unsigned integer. *** sizeof is not a function and it will not generate the execution-time overhead of a function call.
Function fseek( ) • The ANSI standard for fseek as: • Int fseek ( FILE *stream, long int offset, int whence ); • Where offset is number of bytes from location whence in the file pointed by the stream. • whence can take one of the three values: • SEEK_SET, SEEK_CUR, and SEEK_END.
Other library functions • fgetc – reads a character from a specified file. • fgets -- reads a line from … • It reads one character from a file. • fgetc( stdin) is equvalent to getchar ( ) • Similarly, we have fputc function. • feof determines where the end-of-file indicator has been set. Like the statement: • while( !feof (stdin )) in the program to create a sequential access file.
Summary about C file • Functions related to FILE need to be familiar with: • fopen, fcolse, fwrite, rewind, feof, • fscanf, fprintf, fgetc, fputc, fseek,