Presentation is loading. Please wait.

Presentation is loading. Please wait.

C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.

Similar presentations


Presentation on theme: "C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar."— 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 Stan Miller Randy Walter File Randy Walter Record 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: /week10/copen.c fscanf( cfPtr, “%d%s%lf”, &account, name, &balance);

8 More Example Credit inquiry program. week10 credit.c rewind ( cfPtr)

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

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

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


Download ppt "C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar."

Similar presentations


Ads by Google