c file i o what we have done so far
play

C: File I/ O What we have done so far We have been reading/ - PowerPoint PPT Presentation

C: File I/ O What we have done so far We have been reading/ writing files using stdin/ stdout with the redirect operator stdin: Standard input stream stdout: Standard output stream e.g. $ myApplication <


  1. C: File I/ O

  2. What we have done so far … • We have been reading/ writing files using stdin/ stdout with the ‘redirect’ operator • stdin: • Standard input stream • stdout: • Standard output stream e.g. $ myApplication < input.txt • It should be no surprise that C treats file data as a ‘stream’ of bytes

  3. The FILE struct typedef struct _iobuf { void* _Placeholder; } FILE; When opening a file, it is basically giving you have a void pointer (a pointer of undefined type), so you can read any type of data However, you have to provide C some basic information about what you want to do with the file

  4. Basic commands Opening a file: FILE *fopen( const char * filename, const char * mode ); Modes: “r”: Opens an existing text file for reading purpose. “w”: Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file. “a”: Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content. “r+”: Opens a text file for both reading and writing. “w+”: Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. “a+”: Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

  5. Binary vs Text files Text files have a reliable EOF marker so you can read the file without knowing the size ahead of time Binary files do not contain EOF, and you must keep track of the number of bytes you read Also, if opening a binary file, append ‘b’ to the mode commands in fopen e.g. “ rb ”, “ w+b ” etc. Be careful about mixing up the two • Data translation errors (e.g. <cr>-<lf> are valid data in binary files; EOF is valid data) • Numbers can be stored compactly in binary (vs. text)

  6. Read/ Write binary files Calculate size FILE* pFile = fopen(file, "rb"); fseek(pFile, 0, SEEK_END); FILE* pFileOut = fopen(file, "wb"); int size = ftell(pFile) ; Int res= 0; fseek(pFile, 0, SEEK_SET);//Reset to beginning int res = fread(buffer,1,BUFF_SIZE, pFile); Int toWrite = -1; //Need to calculate bytes to write out … //Write BUFF_SIZE byte or less (track how many written/ left) … <your code here> res = fwrite(buffer, 1, toWrite, pFileOut);

  7. Always close what you open … You can get file handle leaks, just like memory leaks If you use fopen, always remember to use fclose! Int fclose(FILE* filePointer);

  8. Reading/ Writing to/ from a file Reading one character at a time: int fgetc( FILE * fp ); //returns a single character Reading a string: char *fgets( char *buf, int n, FILE *fp ); //You must allocate memory for ‘ buf ’, including the NULL Writing one character: int fputc( int c, FILE *fp ); Writing a string: int fputs( const char *s, FILE *fp ); Plenty of others: Review online resources https://www.cprogramming.com/tutorial/cfileio.html https://www.tutorialspoint.com/cprogramming/c_file_io.htm

Recommend


More recommend