Aims and Objectives
Files
Before a File can be Opened...
Closing Files
Reading from a stream
Writing to a stream
Reading from Standard Input
Before a File can be Opened...
FILE *fp;
fp = fopen(const char *filename, const char *mode);
filename is a character string representing the file name. The full pathname of the file must be given if the file is not in the same directory as the executable
mode is one of the following character strings
"r": read only
"w": write only - file is created if it doesn't exist. File is truncated to
zero length
"a": Append - write to file from EOF
"r+": Read and write
"w+": Truncate file to zero length and then allow read and write
"a+": Same as "r+", but write at EOF
fclose(FILE *stream);
char c; FILE *fp; fp = fopen("filename", "r"); do { c = fgetc(fp); } while (c != EOF);
... which is why in my earlier examples for getchar() and putchar() I used int c...
stdin: Standard input
stdout: standard output
stderr: Standard error
fgetc(stdin)
fputc(stdout)
fputc(stderr)
FILE *fp; if (user_input_file == NULL) { fp = stdin; } else { fp = fopen(user_input_file, "r"); } // remember to check status of fp! if (!fp) { handle_fp_error(); exit(1); }
filecopy -i infile
will use infile as the input file, but
filecopy
will take data from the keyboard through stdin