This code does as follows:
1. Reads in two file names (lines 18-22).
2. Then copies data line by line from
fin to stdout (lines 24-29). This would usually display on your tty.
3. An output file named with the second file name the user entered will be opened (lines 31-32).
Now things become somewhat confusing:
4. A file named by the string as read in by the last getline() statement in line 28 will be opened. There may be a great chance, that no such file exists, so that line 33 would fail.
5. Now you write the second file name the user initially entered into the output file you've opened in line 31-32 (line34).
6. Lines 36-37 will fail because opening
fin may have failed in line 33 (see above).
7. ...
To copy the contents of one file to another you may
1. Open the source (
fin) for input (like in line 24).
2. Open the destination (
fout) for output (like in line 32).
3.1. Enter a loop reading line by line until eof or error (like line 28).
3.2. After reading a line you should write it to your output file:
1 2
|
while (fin.getline(buf, 512))
fout << buf << endl;
|
4. Finally after leaving the loop you should close
fin and
fout.
That's it!