CS113%20Introduction%20to%20C - PowerPoint PPT Presentation

About This Presentation
Title:

CS113%20Introduction%20to%20C

Description:

Normally three standard streams are automatically open: ... office hours for the next week to allow you to come and talk to me about the ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 8
Provided by: ioannisa
Category:

less

Transcript and Presenter's Notes

Title: CS113%20Introduction%20to%20C


1
CS113Introduction to C
  • Instructor Ioannis A. VetsikasE-mail
    vetsikas_at_cs.cornell.edu
  • Lecture 11 September 18

2
The C preprocessorConditional 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
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
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 fpfopen(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
Some other functions
  • Defined in ltstdio.hgt
  • 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 KR book

6
Example (from KelleyPohl - modified)
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • void double_space(FILE ifp, FILE ofp)
  • int c
  • while ((cgetc(ifp)) ! EOF)
  • putc(c, ofp)
  • if (c\n)
  • putc(\n, ofp) / duplicate Newline /
  • int main(int argc, char argv)
  • FILE ifp fopen(argv1, r)
  • FILE ofp fopen(argv2, w)
  • double_space(ifp, ofp)
  • fclose(ifp)
  • fclose(ofp)

7
Announcements
  • Wednesday Mock quiz
  • Friday Quiz (the real one) Make sure you come
  • We are done with covering the KR 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 Ill 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
Write a Comment
User Comments (0)
About PowerShow.com