12
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

Embed Size (px)

Citation preview

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

C File Processing

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

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.

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

Date Hierarchy

Tom Green

Judy Smith

Stan Miller

Randy Walter

File

Randy Walter Record

Randy Field

01010010 Byte (ASCII Character R)

1 Bit

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

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.

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

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

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

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.

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

Open an Existing File

• Example: /week10/copen.c

• fscanf( cfPtr, “%d%s%lf”, &account, name, &balance);

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

More Example

• Credit inquiry program.

• week10 credit.c

• rewind ( cfPtr)

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

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.

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

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

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

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

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

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.