330 likes | 538 Views
File Processing. Data hierarchy Sequential access files Text & binary files Random access files. Data Hierarchy. Byte 8 bits (ASCII character ‘A’ = 01000001) Field Group of characters (character string “Fred”) Record Composed of related fields (struct) File
E N D
File Processing • Data hierarchy • Sequential access files • Text & binary files • Random access files
Data Hierarchy • Byte • 8 bits (ASCII character ‘A’ = 01000001) • Field • Group of characters (character string “Fred”) • Record • Composed of related fields (struct) • File • Group of related records (student record file) • Database • Group of related files (students, faculty, and staff files)
Records & Files • Record is a group of related fields • Special field called a “record key” • Used to help retrieve a specific record • File is a group of related records • Sequential file stores records in order by the record key field • C views a file as a sequential stream of bytes • Files end with an end-of-file (EOF) marker
End-of-File Marker • File ends with a end-of-file (EOF) marker • EOF key combinations on different systems: • UNIX systems: <ctrl>d • IBM PC and compatibles: <ctrl>z • Macintosh: <ctrl>d
Streams • 3 streams (file pointers) are automatically provided • Standard input stream to keyboard (stdin) • Standard output stream to screen (stdout) • Standard error stream to screen (stderr) • See example at streams.c • Can redirect the input (<) & output (>) to specified files (use >> to append output): ./a.out <char.txt >output.txt
Sequential-access Files • Individual records are not fixed in length • Have to do a search through the entire file to find something
Opening a File • fp is a pointer to a FILE (file structure) FILE *fp; • fopen returns a pointer to a FILE • 1st argument: name of the file • 2nd argument: how to use the file (read, write, etc.) • If error (such as no such file exists) will return NULL FILE *fopen(char *name, char *mode); • Use in an actual program: fp = fopen("file.txt","w")
File Structure • Opening a file returns a pointer to a file structure • Contains information used to process the file • Location of a buffer • Current character position in the buffer • Whether file is being read or written • Whether errors or EOF have occurred • File pointers for standard input, standard output, and standard error: • stdin, stdout, stderr
r Open a file for reading w Open or create a file for writing If file already exists, erase the contents a Append: open or create a file Write to the end of the file r+ Open a file for update (reading & writing) w+ Open or create a file for update Erase contents of existing file a+ Append: open or create a file for update All writing done at end of file File Modes
More File Functions • int feof(FILE *fp) • Returns non-zero, if end-of-file • Returns 0, if not end-of-file • int fclose(FILE *fp) • Closes a file • Returns 0, if successful; EOF, if not • Will be done automatically after a program finishes executing
Writing to a File • int fputc(int ch, FILE *fp) • Writes one character to a file • If successful, returns the character; if not, returns EOF • int fputs(char *str, FILE *fp) • Writes str to a file • Returns a nonnegative value or EOF for error • Does not append a \n • int fprintf(FILE *fp, const char *format, arg1, arg2,… ) • Same as printf function
Writing to a File #include <stdio.h> int main(){ FILE *fp; char ch = '\0'; if((fp = fopen("file.txt","w"))==NULL) fprintf("File could not be opened\n"); else{ /*read from keyboard*/ while((ch=fgetc(stdin))!=EOF) fputc(ch, fp); /*write to a file*/ close(fp); } return 0; } //See write.c
Reading from a File • int fgetc(FILE *fp) • Reads one character from a file & returns it • char *fgets(char *str, int max, FILE *fp) • Reads a line from a file • Puts the characters (including \n) and \0 in str • Max is the maximum number of characters to be written to the string • int fscanf(FILE *fp, const char *format, arg1, arg2,… ) • Same as scanf function
Buffer Overflow Attack • Don’t use: char* gets(char *s); • Reads one line from standard input & puts it into array “s”. Includes blank spaces. • Should not be used because of the potential for “buffer overflow” attack • Instead use: char* fgets(char* s, int n, FILE* stream); • Example: • char str[10]; fgets(str, sizeof(str), stdin); printf(str);
Reading from a File #include <stdio.h> int main(){ FILE *fp; char ch = '\0'; if((fp = fopen("file1.txt","r"))==NULL) printf("File could not be opened\n"); /*error, such as file does not exists*/ else{ while((ch=fgetc(fp))!=EOF)/*read file*/ fputc(ch, stdout); /*screen output*/ close(fp); } return 0; } //See read.c
Rereading a File • Once a file is read, the file position pointer is at the end of the file • File position pointer • Actually an integer value • Byte location in file for next read or write • Also called file offset • Part of FILE structure • int rewind (FILE *fp) • Reposition to beginning of file (byte 0)
What Does This Program Do? (exercise1.c) #include <stdio.h> int main(){ FILE *fp; char ch='\0'; int i = 0; if((fp = fopen("file.txt","r"))==NULL) printf("File could not be opened\n"); else{ while((ch=fgetc(stdin))!=EOF){ while(!feof(fp)) if(ch==fgetc(fp)) i++; if(ch != '\n') printf("%d\n", i); i=0; rewind(fp); } close(fp); } return 0; }
Text & Binary Files • Text files • Store ASCII characters (ex: 32 = 00110011 00110010) • ASCII uses only 7 bits, 8th bit is for error control • Easy to read, but take up more space • Examples: file.txt, file.html • Binary files • Store numbers (ex: 32 = 100000) • Compact, but more complex to read • Examples: file.gif, file.exe
Binary & Text Files • Add a ‘b’ for binary file modes • rb, wb, ab, rb+, wb+, rb+ • Possible errors • Reading/transferring a binary file as a text file may corrupt the 8th bit • In UNIX, no difference • In DOS/Windows a file can be opened in binary or text mode
Random-Access Files • Random-access files • Individual records are fixed in length • Can be accessed directly without searching though other records • Much quicker to access data • Use record key to find distance from beginning of file • Update & delete data without overwriting other data in the file
Creating • Function fwrite() sends a block of memory to the specified file • size_t fwrite(void *buffer, size_t size, size_t num, FILE *fp); • buffer = start of memory • size = size of each chunk of memory (in bytes) • num = number of chunks of memory (in bytes) • fp = the file to send the memory to • Returns the number of chunks written
/*Creating a file for 100 fixed-length records*/ #include <stdio.h> struct client{ int acctNum; char lastName[15]; char firstName[10]; double balance; }; int main() { int i; struct client blank = { 0, "", "", 0.0 }; FILE *fp; if ((fp=fopen("credit.txt","w"))==NULL) printf("File could not be opened.\n"); else { for ( i = 1; i <= 100; i++ ) fwrite(&blank,sizeof(struct client),1,fp); fclose(fp); } return 0;} //See create.c
Writing Data • Use fseek() to position the file position pointer • int fseek(FILE *stream, long int offset, int whence) • stream = file pointer • offset = number of bytes from “whence” • whence = • SEEK_SET (beginning of file) • SEEK_CUR (current location) • SEEK_END (end of file)
/*Writing to a file*/ FILE *fp; struct client temp = { 0, "", "", 0.0 }; if ((fp = fopen("credit.txt","r+"))==NULL) /*note: using "w" will overwrite the data*/ printf("File could not be opened.\n"); else { printf("Enter account number (1 to 100):"); scanf( "%d", &temp.acctNum ); while ( temp.acctNum != 0 ) { printf("Enter last, first & balance: "); scanf("%s%s%lf", temp.lastName, temp.firstName, &temp.balance ); fseek(fp,( temp.acctNum - 1 ) * sizeof( struct client ), SEEK_SET ); fwrite(&temp,sizeof(struct client),1,fp); printf( "Enter account number:" ); scanf( "%d", &temp.acctNum ); } fclose( fp );} //See writing.c
Reading Data • Use fread() to read a specific number of bytes from a file into memory • size_t fread(void *buffer, size_t size, size_t num, FILE *fp); • buffer = start of memory • size = size of each chunk of memory (in bytes) • num = number of chunks of memory (in bytes) • fp = the file from which the memory is read • Returns the number of chunks written
/*Reading from a file*/ FILE *fp; struct client temp = { 0, "", "", 0.0 }; if((fp=fopen("credit.txt","r"))==NULL) printf("File could not be opened.\n" ); else { printf( "%-6s%-16s%-11s%10s\n", "Acct", "Last Name","First Name", "Balance" ); while(!feof(fp)){ fread(&temp,sizeof(struct client),1,fp ); if(temp.acctNum!=0) printf("%-6d%-16s%- 11s%10.2f\n", temp.acctNum, temp.lastName, temp.firstName, temp.balance ); } fclose(fp); } //See reading.c
Updating a Record • Ask the user for the record for updating printf("Enter account to update (1-100):"); scanf( "%d", &account ); • Adjust the file position pointer & read the data into temp fseek( fp,( account - 1 ) * sizeof( struct client ), SEEK_SET ); fread( &temp, sizeof( struct client ), 1, fp );
Updating a Record • Check to see if record exists or not if ( temp.acctNum == 0 ) printf("Acount #%d has no information.\n", account ); • If so, display to screen else { printf( "%-6d%-16s%-11s%10.2f\n\n", temp.acctNum, temp.lastName, temp.firstName, temp.balance );
Updating a Record • Ask user for deposit or withdrawal printf( "Enter charge ( + ) or payment ( - ): " ); scanf( "%lf", &transaction ); temp.balance += transaction; • Write new data to file fseek( fp, ( account - 1 ) * sizeof( struct client ), SEEK_SET ); fwrite( &temp, sizeof( struct client ), 1, fp );
Deleting a Record • Ask the user for the record for deleting printf("Enter account to delete (1-100):"); scanf( "%d", &account ); • Adjust the file position pointer & read the data into temp fseek( fp,( account - 1 ) * sizeof( struct client ), SEEK_SET ); fread( &blank, sizeof( struct client ), 1, fp );
Deleting a Record • Check to see if record exists or not if ( temp.acctNum == 0 ) printf("Acount #%d has no information. \n", account ); • If so, delete it (write over it with empty record) else { fseek( fp, ( accountNum - 1 ) * sizeof( struct client ), SEEK_SET ); fwrite(&blank, sizeof(struct client), 1, fp ); }
Example Program • Transaction-Processing Program • See fig11_16.c from D & D textbook • Program has 5 options: • Store the list in accounts.txt • Update an account • Add a new account to the file • Delete a record • End program