1 / 18

C Preprocessing File I/O

C Preprocessing File I/O. C Preprocessor. Modifies C code "to save typing" Define constants Define macros Include files Other parameters (time of compilation...). Preprocessor constants. Define a symbolic constant like so #define PI 3.141526 Better version

soren
Download Presentation

C Preprocessing File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Preprocessing File I/O

  2. C Preprocessor • Modifies C code "to save typing" • Define constants • Define macros • Include files • Other parameters (time of compilation...)

  3. Preprocessor constants Define a symbolic constant like so #define PI 3.141526 Better version #define PI ( 3.141526 ) Use the symbolic constant circle_length = 2 * PI * radius ;

  4. Preprocessor constants (2) Check if constant defined ( #ifdef ) #define VERBOSE . . . #ifdef VERBOSE printf("I am extremely glad to see you !\n"); #else printf("Hi !\n"); #endif

  5. Preprocessor Macros Parameterized Macros: Similar to function calls. Symbolic parameters ! #define SQUARE( x ) x * x Better version: #define SQUARE( x ) ((x) * (x)) Usage:What will be the output for each version? int x x = SQUARE ( 1 + 2 + 3 );  (1+2+3*1+2+3) =??? printf( " x = %d \n", x ); is x=11?, or is it, x=36? How do you fix it to generate 36? ((1+2+3) * (1+2+3))

  6. Including files • Used to include header files • Can be used to include any file • Beware of including header files twice #include "MyFileName.c"

  7. Header files • Usually define function prototypes, user defined types and global variables. • Avoid including twice int x; /* included from myHeader.h */ int x; /* included from myHeader.h */ • Standard header file header #ifndef MyHeaderFile_H #define MyHeaderFile_H ... /* header file contents goes here */ #endif

  8. /* example.c */ #include <stdio.h> #define ONE 1 main(){ if(ONE != 1) return 0; printf("The answer is %d.\n", myfunc(2,6) ); } myfunc( int a, int b) { int i = ONE, j = ONE; for( ; i <= b; ++i) j = j * a; return j; } See this Example

  9. File Input / Output

  10. What is a File • A file is a collection of related data • "C" treats files as a series of bytes • Basic library routines for file I/O #include <stdio.h>

  11. Basics About Files • Files must be opened and closed #include <stdio.h> . . . FILE * myFile; myFile = fopen ("C:\\data\\myfile.txt", "r");// Name, Mode (r: read) if ( myFile == NULL ){// (w: write) /* Could not open the file */ ... } . . . fclose ( myFile ); Note: status = fclose(file-variable) status = 0 if file closed successfully- Error otherwise.

  12. End-line Character • Teletype Model 33 (long time ago...) used 2 characters at the end of line. • RETURN character • LINE FEED character • Computer age • UNIX: LINE FEED at the end of line: "\n" • MS-DOS/Windows: both characters: "\n\r" • Apple: RETURN at the end of line: "\r"

  13. File Types • Text (ASCII) files • Binary files • Special (device) files stdin - standard input (open for reading) stdout - standard output (open for writing) stderr - standard error (open for writing)

  14. Operations with Files • Writing (w) • sequential • random • appending (a) • Reading (r) • sequential • random • fopen() revisited • FILE *fOut; • fOut = fopen("c:\\data\\log.txt", "w" );

  15. Useful File I/O Functions • fopen(), fclose() -- open/close files • fprintf ( myFile, "format...", ...) -- formatted I/O • fscanf ( myFile, "format...", ...) • fgets(), fputs() -- for line I/O • fgetc(), fputc() -- for character I/O • feof() -- end of file detection, when reading

  16. Binary and Random I/O • Binary I/O readSize = fread(dataPtr, 1, size, myFile); //size of data read, if < size then encountered an error. writeSize = fwrite(dataPtr, 1, size, myFile); • Positioning for random I/O fseek(myFile, 0, SEEK_SET); fseek(myFile, 10, SEEK_CUR); fseek(myFile, 0, SEEK_END);

  17. Buffered v.s. Unbuffered I/O //no immediate write to file, instead buffer data and then flush after program finished • Buffered I/O may improve performance • Buffered I/O is with f...() functions • fopen(), fwrite() • Unbuffered I/O • open(), write()

  18. Example: En/De-Crypter int main ( int argc, char * argv[] ) { FILE *in, *out; in = fopen ( argv[1], "rb"); out = fopen ( argv[2], "wb"); if ( ! in | | ! out ){ printf( "Error opening files ! \n" ); return -1; } while( ! feof ( in ) ){ ch = fgetc ( in ); fputc ( (ch ^ 0xFF) , out ); //UTF-16 vs UTF-8(Unicode Byte Order mark) } //Unicode Transformation Format return 0; }

More Related