CS110_09a_binaryFiles - EECS110: 9a Binary File I/O,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 9a Binary File I/O, Modules Revisited Jack Tumblin jet@cs.northwestern.edu I hope you: Could pass a final exam (we don't have one) Will fill out online CTEC forms for this course 1 Files in C File system details vary widely-- widely every operating system does it differently. UNIX, Linux, IRIX, HP-UX, VMS, Be, MS-DOS, Win16, Win32, NTFS, HPFS, AppleOS, MacOS, SunOS, Solaris, ... But in C, file functions are always the same: C ALWAYS uses a file-control type FILE, ALWAYS supports these basic file functions: fopen(), fclose() open/close a file, fscanf(), fprintf() opened text file read/write fread(), fwrite() read/write to any open file. New! Know these 6--all the rest are minor details! (others: feof(),ferror(),clearerr(), ftell(),rewind(),fseek(),remove(),etc.) 2 (Recall) File Open, File Close ALWAYS use pointer-to-FILE variable ALWAYS open a file before use, ALWAYS check for file-opening error before use, ALWAYS close it again when finished, // declare pointer-to-FILE variable FILE *pF; pF = fopen("myfile.txt","r"); // open file if(pF==NULL) { printf("\n Error on file open!!\n\n); ... } // . . . Read, write, etc . . . // and after you're done fclose(pF); 3 File Open, File Close NOTES: Open/Close does all file system `housekeeping' FILE pointer is your only `handle' on the file No `pointer math' allowed on FILE pointers!!! fclose(pF); fclose(pF+1); !ERROR! fopen()reserves memory (for buffers, counters, etc.): forget to fclose() files? Big memory leak!! 4 // good! Binary File Read: fread() Copies bytes from file to memory YOU must ensure enough memory! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data read from file // open file for reading pF = fopen("myfile.qzk","r"); if(NULL==pF) return(-1); //(error check) n = 32; cnt = fread(buf, sizeof(int), n, pF); Open file for reading (exit on error) 5 Binary File Read: fread() Copies bytes from file to memory YOU must ensure enough memory! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data read from file // open file for reading pF = fopen("myfile.qzk","r"); if(NULL==pF) return(-1); //(error check!) n = 32; cnt = fread(buf, sizeof(int), n, pF); (Better Idea: dynamic allocation for `buf') Pointer to destination buffer Holds at least `n' items of your chosen type. (using `int' here).. 6 Binary File Read: fread() Copies bytes from file to memory YOU must ensure enough memory! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data read from file // open file for reading pF = fopen("myfile.qzk","r"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fread(buf, sizeof(int), n, pF); Size of items we will read (in bytes) (using `int' here) 7 Binary File Read: fread() Copies bytes from file to memory YOU must ensure enough memory! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data read from file // open file for reading pF = fopen("myfile.qzk","r"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fread(buf, sizeof(int), n, pF); Number of items to read from file 8 Binary File Read: fread() Copies bytes from file to memory YOU must ensure enough memory! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data read from file // open file for reading pF = fopen("myfile.qzk","r"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fread(buf, sizeof(int), n, pF); Opened pointer-to-FILE we will read from 9 Binary File Read: fread() Copies bytes from file to memory YOU must ensure enough memory! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data read from file // open file for reading pF = fopen("myfile.qzk","r"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fread(buf, sizeof(int), n, pF); if(cnt!=n) return(-1); //(error!) Return Value: Number of items successfully read 10 Binary File Write: fwrite() Copies bytes from memory to file Syntax and usage matches fread() ! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data written to file // open file for writing pF = fopen("myfile.qzk","w"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fwrite(buf, sizeof(int), n, pF); if(cnt!=n) return(-1); //(error!) Open file for writing (exit on error) 11 Binary File Write: fwrite() Copies bytes from memory to file Syntax and usage matches fread() ! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data written to file // open file for writing pF = fopen("myfile.qzk","w"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fwrite(buf, sizeof(int), n, pF); if(cnt!=n) return(-1); //(error!) Pointer to source buffer Holds `n' items of ANY one type. (using `int' here) 12 Binary File Write: fwrite() Copies bytes from memory to file Syntax and usage matches fread() ! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data written to file // open file for writing pF = fopen("myfile.qzk","w"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fwrite(buf, sizeof(int), n, pF); if(cnt!=n) return(-1); //(error!) Size of items we will write (in bytes) (using `int' here) 13 Binary File Write: fwrite() Copies bytes from memory to file Syntax and usage matches fread() ! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data written to file // open file for writing pF = fopen("myfile.qzk","w"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fwrite(buf, sizeof(int), n, pF); if(cnt!=n) return(-1); //(error!) Number of items to read from file 14 Binary File Write: fwrite() Copies bytes from memory to file Syntax and usage matches fread() ! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data written to file // open file for writing pF = fopen("myfile.qzk","w"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fwrite(buf, sizeof(int), n, pF); if(cnt!=n) return(-1); //(error!) Opened pointer-to-FILE we will write to 15 Binary File Write: fwrite() Copies bytes from memory to file Syntax and usage matches fread() ! FILE *pF; int buf[40]; int n,cnt; // declare a pointer-to-FILE // holds data written to file // open file for writing pF = fopen("myfile.qzk","w"); if(NULL==pF) return(-1); //(error!) n = 32; cnt = fwrite(buf, sizeof(int), n, pF); if(cnt!=n) return(-1); //(error!) Return Value: Number of items successfully written 16 fread(), fwrite() Notes Read/write `items' can be entire data structures !CAREFUL! pointer values are not repeatable-won't work if you read/write them to files! And as always, **! Never over-run buffers !** Different operating systems use different number storage formats: Windows98 (int)!= Solaris (int) 17 Strings and fwrite() Subtle Mistake: forgot to write string terminator Recall strlen(msg) DOES NOT count null char `\0' that ends the string at pointer msg! int cnt; FILE *pF; // declare a pointer-to-FILE char msg[40]="This is my song!\n"; // string pF = fopen("music.mdi","w"); cnt= fwrite(msg, sizeof(char), strlen(msg),pF); BUG!--incomplete string write-Does not write the string's null terminator to the file 18 Strings and fwrite() Subtle Mistake: forgot to write string terminator Recall strlen(msg) DOES NOT count null char `\0' that ends the string at pointer msg! int cnt; FILE *pF; // declare a pointer-to-FILE char msg[40]="This is my song!\n"; // string pF = fopen("music.mdi","w"); cnt= fwrite(msg, sizeof(char),1+strlen(msg),pF); FIXED!--add one to strlen() to include string's null terminator 19 Strings and fread(),fwrite() Text File == File filled with nothing but strings Text files are much trickier than numerical files What is max string length in a file? ?we don't know until we read the file?!?! Book presents old, clunky and tedious low-level text file I/O functions: getc(), ungetc(),gets(), putc(), puts(), . . . Learn them to read others' code, But scanf, printf variants do it all for you! 20 Other File Functions Rename file err = rename(char *old,char *new); Delete file err = remove(char *old); Create temp filename char *name = tmpnam(NULL); `Rewind' file rewind(FILE *pFile); Others: ftell, fseek, ferror, feof,... See `stream I/O fcns' for complete, detailed explanations. And with that we're finished with C! 21 22 Wrap-Up: Final Study and Review Review Lecture Notes thoroughly Review Assigned Reading Review, Help Sessions in class Tues, Fri. Practice Problems at end of each Chapter Review Appendices at end of book . . . 23 ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online