Take the 2-minute tour ×
#include <iostream> 
#include <fstream> 
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[]) 
    ifstream is; 
    ofstream outfile;
    char ch; 
    while (1) 
         ch = is.get();   // this is where test.txt is supposed
         outfile.put(ch); // to be copied to test2.txt
         if (is.eof()) 
         cout << ch;  //this shows


    ifstream outfile2;
    char ch2; 
    while (1)
       ch2 = outfile2.get(); 
       if (outfile2.eof()) 
       cout << ch2;  //this doesnt

        return 0; 

I run it through cmd giving it 2 arguments test.txt test2.txt and it outputs what i have written in test.txt in cmd but test2.txt remains empty for some reason?

share|improve this question
add comment

2 Answers

for me its not coming blank but with some extra appended characters. this is because you are writing the character which you got from the old file to the new one before checking eof().

the code for writing from one file to another should be changed as

while (1) 
         ch = is.get();
         if (is.eof()) 
         cout << ch;  //this shows
share|improve this answer
Same thing happens for me...nothing in test2.txt –  Alex Moumoulidis May 22 '13 at 18:31
change the index from argv[1], argv[2] to argv[0], argv[1] along with the code change suggested above –  Saksham May 22 '13 at 18:34
the arguments that i pass must be in argv[1] and argv[2] right? –  Alex Moumoulidis May 22 '13 at 18:39
isnt argv[0] used for the program name or something like that? –  Alex Moumoulidis May 22 '13 at 18:40
okay right. i usually work on visual studio and on that it is working fine by replacing arguments with hard coded file names –  Saksham May 22 '13 at 18:47
show 2 more comments

Please check the streamstate not just for eof() but also for failure. Also, after reading the last character, it wouldn't be uncommon if the streamstate was EOF even though the character was successfully read. Therefore, always try to read an element and if it succeeded, and only then, use the element:

ifstream in(argv[1]);
ofstream out(argv[2]);
char c;

To make this really efficient, use this though:

out << in.rdbuf();

In any case, check the streamstate for success:

    throw std::runtime_error("failed to read input file");
    throw std::runtime_error("failed to write output file");
share|improve this answer
the program is running fine in visual studio but not using gcc. it's throwing "this program cannot be run in DOS mode" –  Saksham May 22 '13 at 19:31
Which program are you talking about? –  Ulrich Eckhardt May 22 '13 at 19:35
@Alex code replaced with my snippet. see stackoverflow.com/questions/16700102/… –  Saksham May 22 '13 at 19:44
Saksham, please check where and to whom you are replying, I guess you didn't mean your message to show up in the context of my answer but either in that of the question or your answer. –  Ulrich Eckhardt May 22 '13 at 20:09
Ok so ill do that only could you make an assumption on why whatever i have inside test.txt isnt copied in test2.txt although the program seems to run fine on cmd and it also outputs it? –  Alex Moumoulidis May 22 '13 at 20:19
add comment

Your Answer


