copy from one file to another

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;


int main(int argc, char *argv[])
{
char c, c1, c2;
ifstream inFile;
ofstream outFile;

inFile.open("blackbird.txt");

if (inFile.fail())
{
cout << "\nThe file was not successfully opened for reading."
<< "\nPlease check that the file currently exists.\n\n";
exit(1);
}

cout << "\nThe file has been successfully opened for reading.\n\n";


outFile.open("blackbird_copy.txt");

if (outFile.fail())
{
cout << "The file was not successfully opened for writing" << endl;
exit(1);
}

cout << "The file has been successfully opened for writing.\n\n";


//outFile << "Hello";

// 1) this loop doesn't terminate. 2) the computer doesn't know what c1 is.
/*
while (inFile.get(c1))
{
outFile << c1;
cout << c1;
}
*/



// This one is no better
/*
while (inFile.good())
{
inFile.get(c);
outFile << c;
}
*/


inFile.close();
outFile.close();


// read contents of blackbird_copy to check our work.

inFile.open("blackbird_copy.txt");

while (inFile.get(c2))
{
cout << c2;
}

cout << endl << endl;
return 0;
}


I don't understand this comment:
 
// 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.
1
2
3
    ifstream inFile("blackbird.txt",       ios::binary);
    ofstream outFile("blackbird_copy.txt", ios::binary);
    outFile << inFile.rdbuf();

Last edited on
I meant that the code compiles, but the loop is infinite and I have to stop the program manually (exit code 9).

I wonder why it worked for you but not for me. Thanks for your comment on the alternative, I hadn't thought of that.
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?

How about you try this loop :
1
2
3
4
5
do {
  c1 = inFile.get();
  // outFile << c1;
  cout << c1;
}while(inFile.good());
... to read a file instead? It is not the most optimal solution, however if it fixs your problem, I will tell you some more things about it.
closed account (48T7M4Gy)
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.

That's correct.
When one uses the default constructor for ifstream and ofstream the appropriate in and out flags are automatically used, so there's no need to explicitly restate them.
http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
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.
Last edited on
beefheart wrote:
I meant that the code compiles, but the loop is infinite and I have to stop the program manually (exit code 9)

When you do this, what is displayed by the line cout << c1;? How does it compare to the contents of your file?

1
2
3
4
5
6
7
8
// 1) this loop doesn't terminate. 2) the computer doesn't know what c1 is.
/*
while (inFile.get(c1))
{
outFile << c1;
cout << c1;
}
*/

Last edited on
Topic archived. No new replies allowed.