{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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

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

View Full Document Right 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.
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 >.
Image of page 3

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

View Full Document Right 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);
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; }
Image of page 5

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

View Full Document Right 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).
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern