1 / 16

C File Processing

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.

manon
Download Presentation

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. C File Processing

  2. 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.

  3. Date Hierarchy Tom Green Judy Smith File Stan Miller Randy Walter Record Randy Walter Randy Field 01010010 Byte (ASCII Character R) 1 Bit

  4. 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.

  5. 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

  6. 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.

  7. Open an Existing File • Example: /week012/creat_sfile.c • fscanf( cfPtr, “%d%s%lf”, &account, name, &balance);

  8. More Example • Credit inquiry program. • week012 credit.c • rewind ( cfPtr); • To move the file pointer to the beginning of the specified file.

  9. 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.

  10. C’s view of a random-access file 300 400 100 200 0 Byte offset Each record has 100 bytes

  11. 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

  12. 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

  13. 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.

  14. 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.

  15. 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.

  16. Summary about C file • Functions related to FILE need to be familiar with: • fopen, fcolse, fwrite, rewind, feof, • fscanf, fprintf, fgetc, fputc, fseek,

More Related