Presentation is loading. Please wait.

Presentation is loading. Please wait.

Console and File I/O - Basics Rudra Dutta CSC 230 - Spring 2007, Section 001.

Similar presentations


Presentation on theme: "Console and File I/O - Basics Rudra Dutta CSC 230 - Spring 2007, Section 001."— Presentation transcript:

1 Console and File I/O - Basics Rudra Dutta CSC 230 - Spring 2007, Section 001

2 Copyright Rudra Dutta, NCSU, Spring, 20072 Input and Output User interaction: reading and writing data – Reading from keyboard or file – Writing to console or file C provides NO language primitives for I/O ! – Library functions provide mechanisms – Standard libraries Complex functions build on simple ones, but… What do simple functions build on? – Direct interaction with OS – “System” functions, provided as library

3 Copyright Rudra Dutta, NCSU, Spring, 20073 Character I/O Standard library treats all I/O as occurring in text streams int getchar() – Reads single next character from the input stream represented by the keyboard – Returns as … int !! int putchar(int c) – Takes the single character c (passed as int !) – Prints it on the output stream which is the console

4 Copyright Rudra Dutta, NCSU, Spring, 20074 Copy Input to Output #include int main() { int c; while ((c = getchar ()) != EOF) { putchar (c); }

5 Copyright Rudra Dutta, NCSU, Spring, 20075 Files General meaning - not console or keyboard, but something on disk Console and keyboard can be seen as special cases – Then all stream input and output can be represented as files Three special files – stdin - keyboard – stdout - default display, console – stderr - usually also default display, logically different Functions actually defined on files – File versions: fgetc(), fputc() – See manual pages for getchar() and putchar() On keyboard, what is End Of File? – A Ctl-D can by typed to indicate EOF

6 Copyright Rudra Dutta, NCSU, Spring, 20076 Shell Redirections Input or output of a program can be redirected from the command line –, | Meaning of redirecting input: – “Provide the program to be run with the contents of this file as if it is coming from stdin” – Similar meaning in redirecting output Piping – “Run both these programs, piping the stdout of the first one into the stdin of the second one”

7 Copyright Rudra Dutta, NCSU, Spring, 20077 Using Text Files stdio.h contains a structure called FILE – Can be used by thinking of it as a type – Indicates a stream Can be opened by fopen(), closed by fclose() – All opened files effectively close when program terminates (usually, this is an OS and not C issue) – But leaving out an fclose() can cause many errors and subsequent headaches Opening a file returns a file pointer – Like a handle, but actually a pointer in the C sense – Must be used to read/write/close

8 Copyright Rudra Dutta, NCSU, Spring, 20078 Formatted I/O printf() and scanf() – Exclusively for stdout and stdin, respectively Consider printf() – Allows programmer to pass a “format string”, and indicate placeholders – Remaining arguments specify values to use for placeholders – Variable number of argument ! scanf() – Also uses format string – Must pass locations of variables whose values will be read into – Use of pointers - we shall revisit appropriately Using text files – fprintf() and fscanf() – Exactly the same, except addition first argument is file pointer

9 Copyright Rudra Dutta, NCSU, Spring, 20079 printf() Conversions Format string needs its own “language” to specify types of variable values – Why? Name of variable indicates only location where variable can be found – Does not indicate type – Therefore, how many bytes it occupies Coversions are listed in manual pages – More accesibly, in text


Download ppt "Console and File I/O - Basics Rudra Dutta CSC 230 - Spring 2007, Section 001."

Similar presentations


Ads by Google