Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 11 : September 18.

Similar presentations


Presentation on theme: "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 11 : September 18."— Presentation transcript:

1 CS113 Introduction to C Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.eduvetsikas@cs.cornell.edu Lecture 11 : September 18

2 2 The C preprocessor: Conditional Inclusion #if, #elif, #else, #endif –Allow us to define conditional statements for what the compiler should compile #define name and #undef name defined( name ) returns 1 if name is defined with a #define statement and 0 otherwise #ifdef and #ifndef are same respectively as #if defined and #if !defined #ifdef DEBUG printf(“Debug mode”); i++; #else printf(“Real mode”); #endif

3 3 Streams I/O done through streams; two kinds: text and binary –Text streams are sequences of lines, each of which is a sequence of characters terminated by a newline –Binary streams are sequences of characters corresponding to internal representation of data. Streams created by opening files and referenced using stream pointers (FILE *) Normally three standard streams are automatically open: –stdin(stream for standard input - from keyboard) –stdout(stream for standard output - to screen) –stderr(stream for standard error output - to screen)

4 4 Functions for opening & closing Open a file: –FILE *fopen(char *name, char *mode); –Opens file with name name using mode mode –Mode is taking values: “r” (open text for reading), “w” (open text for writing), etc… –For binary files add “b” at the end, e.g. “rb” (open binary for reading) –Usage: FILE *fp=fopen(“infile”, “r”); –Returns NULL if some problem occurs during the opening of the file (e.g. file is not found, or permissions are not set correctly etc.) Close a file: –int flcose(FILE *fp);

5 5 Some other functions Defined in int fprintf(FILE *fp, char *format, …); int fscanf(FILE *fp, char *format, …); int fgetc(FILE *fp); int fputc(int c, FILE *fp); char *fgets(char *s, int n, FILE *fp); int fputs(char *s, FILE *fp); int feof(FILE *fp); For more functions read appendix B of the K&R book

6 6 Example (from Kelley&Pohl - modified) #include void double_space(FILE *ifp, FILE *ofp) { int c; while ((c=getc(ifp)) != EOF) { putc(c, ofp); if (c==‘\n’) putc(‘\n’, ofp); /* duplicate Newline */ } int main(int argc, char **argv) { FILE *ifp = fopen(argv[1], “r”); FILE *ofp = fopen(argv[2], “w”); double_space(ifp, ofp); fclose(ifp); fclose(ofp); }

7 7 Announcements Wednesday: Mock quiz Friday: Quiz (the real one) – Make sure you come We are done with covering the K&R book Read the appendices by yourselves and look especially at appendix B where the library functions are presented Homework 4 is challenging and you should start as early as possible; I’ll announce extra office hours for the next week to allow you to come and talk to me about the homework I will do some revision, answer questions and do miscellaneous programming (debugging) skills next


Download ppt "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 11 : September 18."

Similar presentations


Ads by Google