My problem is that both the while loops don't work. Please see comments in code below.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
// 1) this loop doesn't terminate. 2) the computer doesn't know what c1 is.
I assume you mean the code does not compile. But if that was the case then you couldn't run it, so could not say what happens to the loop.
However, I ran your program with no problems using this loop:
1 2 3 4 5
while (inFile.get(c1))
{
outFile << c1;
cout << c1;
}
The alternative you proposed as "no better" is in fact worse:
1 2 3 4 5
while (inFile.good())
{
inFile.get(c); // what if this get fails?
outFile << c; // the old value of c is still written to the output.
}
While we're here, there's a way to copy a file in a single statement, though the files should be opened in binary mode to allow it to work with any type of content.
The standards say one should open a file with flag (std::ifstream::in) to read a file and open a file with flag (std:: ofstream:: out) to write a file. If your input file is readable, open-write binary flags might be not needed. How big is your input file and is your input file readable? Could you please show us the contents of the file?
This almost one-liner approach is remarkably simple and at least worth knowing about if not useable in class situations:
http://stackoverflow.com/questions/3512271/how-to-copy-text-file-in-c-or-c
The standards say one should open a file with flag (std::ifstream::in) to read a file and open a file with flag (std:: ofstream:: out) to write a file.
How about you try this loop : do {
c1 = inFile.get();
// outFile << c1;
cout << c1;
}while(inFile.good());
The op already said that very similar code did not fix the problem. Also it contains a logic error as I pointed out above. edit: I missed that you use a version of get() which takes no parameter and returns an int.