Professional Documents
Culture Documents
Lecture11 Files
Lecture11 Files
Files
Types
Data Hierarchy
Harry Potter 169
Angy Green
John Nick John John 01001010
234
File
1 bit
Provide communication channel between files and programs C views files as a sequential stream of bytes When a file is opened, a stream is associated with the file
When a program is executed, three files and their associated streams are opened
Read C How to Program Chapter 11 File Processing Relationship between FILE pointers, FILE structures and File Control Blocks (FCBs)
File Pointer
General Format FILE * pointer-name Eg. FILE *fPtr Keyword FILE establishes a buffer area Pointer indicates beginning of area
Example fPtr = fopen(client.dat, r); fPtr = fopen(client.dat, w); fPtr = fopen(client.dat, a);
File Modes
r w Open for reading Create for writing. If file already exists, discard contents Append. Open or create for writing at end of file Open for reading & writing Create for update. If file already exists, discard contents Append. Open or create for update at end of file
a
r+ w+ a+
If (fPtr == NULL)//results in error Check to determine that file was opened successfully if(fopen(file.txt,a+)==NULL) printf(Error opening file)
A text file stores line endings with the \n character though some Windows use a combination of \r and \n characters. End of file may also be treated with the Ctrl-Z character.
Binary files
Most straightforward Assumes nothing about the OS To declare file mode to be binary include a b in the mode string e.g. wb,
Records typically stored in order by the record key field Data stored sequentially one after the other
Example of Program while (!feof(stdin)) fscanf(writePtr, "%d%s%f",&acct_no,name,&balance); fprintf(writePtr,"%d\t%s\t%.2f\n",acct_ no,name,balance); rewind(writePtr); fclose(writePtr);
Updating Records
New record has more characters than previous Characters beyond the dot would overwrite beginning of next sequential record Problem: formatted input/output using fprintf/fscanf suggests that records can vary in size Entire file is usually re-written (records before the one requiring update copied to a new file, new record written, records after one requiring update copied to new file
Individual records normally fixed in length May be accessed directly without searching through other records
Uses fwrite and fread instead of fprintf and fscanf Often write records rather than individual fields to files (hence uses structures) See sample program
e.g. fwrite(&client1,sizeof(Acct),1,writePtr); pOutArea area in memory where data is currently elementSize how many bytes to be written to the file Count how many of elementSize to write *sp pointer to the file where the data is to be written
fread(&client1, sizeof(Acct),1,writePtr); pInArea pointer to the input area in memory usually a structure elementSize is muliplied by count to determine how many data is to be transferred to memory *sp pointer to the file where the data is
FSEEK
1st parameter is a pointer to an opened file 2nd parameter specifies the number of bytes the position indicator should move (skip) relatively or absolutely (3rd parameter) 3rd parameter specifies the starting point of the seek can either be
SEEK_SET (beginning of file) SEEK_CUR (current position in file) SEEK_END (from the end of the file)
Files
Read C How to Program Chapter 11 File Processing Full menu-based (add, update, delete etc.) transaction processing program