22 - 22. FILE INPUT/OUTPUT File Pointers and Streams...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
22. FILE INPUT/OUTPUT
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File Pointers and Streams • Declarations of functions that perform file I/O appear in <stdio.h>. Each function requires a file pointer as a parameter. • A file pointer is a pointer to a FILE structure (FILE is defined in <stdio.h>). A program may declare as many file pointers as needed: FILE *fp1, *fp2; • A file pointer represents a stream, which may be a file or —in general—any source of input or output.
Background image of page 2
File Pointers and Streams • Three streams are standard: stdin Standard input stdout Standard output stderr Standard error These streams need not be opened or closed. • Standard input and output can be redirected in both UNIX and Windows: prog <data >result • UNIX also allows redirection of the standard error stream. Only certain versions of Windows (NT and 2000) allow this. Redirection of standard error is done by using 2> instead of >.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Opening and Closing Files • Files can be opened by calling fopen: FILE *fopen(const char *filename, const char *mode); mode can be one of the following: "r" Open for reading "w" Open for writing (file need not exist) "a" Open for appending (file need not exist) "r+" Open for reading and writing, starting at beginning "w+" Open for reading and writing (truncate if file exists) "a+" Open for reading and writing (append if file exists) If the file cannot be opened, fopen returns NULL. • Files can be closed by calling fclose: int fclose(FILE *stream);
Background image of page 4
Opening and Closing Files • Example: #include <stdio.h> #include <stdlib.h> #define INPUT_FILE "example.dat" int main(void) { FILE *fp; fp = fopen(INPUT_FILE, "r"); if (fp == NULL) { printf("Can't open %s\n", INPUT_FILE); exit(EXIT_FAILURE); } fclose(fp); return 0; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Command-Line Arguments • The main function may have parameters named argc and argv, which allow access to the command line when the program is executed: int main(int argc, char *argv[]) { } • argc is a count of the number of command line arguments (including the name of the program itself). • argv is an array of pointers to the command line
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/08/2011 for the course CGS 3460 taught by Professor Staff during the Summer '08 term at University of Florida.

Page1 / 20

22 - 22. FILE INPUT/OUTPUT File Pointers and Streams...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online