To use the things described here, #include the <cstdio> library.
Use
FILE* f = fopen(path, "r");to open file path for reading. If it is not possible to open the file, fopen will return NULL. The standards require programs to check for that and, if the file could not be opened, to report it, with the name of the file that could not be opened. |
When you are done reading the file, close it. The standards require you to do that. Parameter f has type FILE*. |
This works just like scanf, but it reads from open file f, which must have type FILE*. |
This expression reads one character from open
file f. It is like getchar,
but reads from an open file.
Never store the result of getc into a variable of type char, because getc can return EOF, which is not a character. |
Write a complete program that takes two file names A and B from the command line. It should create a file called B and copy the entire contents of file A into file B. For example, if the executable program created from your C++ program is called copy, then command
copy myfile.txt myfile.cpymakes a copy of myfile.txt that is called myfile.cpy. Answer