|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e9/solution/f3.c
#include <stdio.h>
/**
* Make the program take two filenames on the command line. Print the usage
* message "f3 <filename> <filename>\n" if not enough command line parameters
* are provided.
* Open the first filename for reading and the second filename for writing.
* Copy the contents of the first file into the second file. It would
* probably be easiest to do this one character at a time, using fgetc and
* fputc.
*/
int main(int argc, char *argv[])
{
if (argc < 3) {
printf("f3 <filename> <filename>\n");
return 1;
}
FILE *in = fopen(argv[1], "r");
if (in == NULL) {
printf("Error opening input file.\n");
return 1;
}
FILE *out = fopen(argv[2], "w");
if (in == NULL) {
printf("Error opening output file.\n");
return 1;
}
int ch;
while((ch = fgetc(in)) != EOF) {
fputc(ch, out);
}
fclose(in);
fclose(out);
return 0;
}
|