C File Processing. Objectives To be able to create, write and update files. To become familiar with...
-
Upload
daniela-hubbard -
Category
Documents
-
view
214 -
download
0
Transcript of C File Processing. Objectives To be able to create, write and update files. To become familiar with...
![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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022082713/56649e575503460f94b503db/html5/thumbnails/12.jpg)
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.