Are you deleting the previous transactionlog.txt somewhere? Else rename will fail because the file already exists.
You should also avoid using eof().
If you change this:
while(!myfile.eof())
{
getline(myfile,temp);
cout << temp;
tempfile << temp << endl;
}
to this:
while(getline(myfile,temp))
{
cout << temp;
tempfile << temp << endl;
}
The eof() function is gone, and it also looks a lot cleaner :)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
The documentation for rename() plainly states that if a file with the new name already exists, results are implementation-defined. You are better off deleting any existing 'new name' file first.
The problem with eof() is more insidious. Imagine what would happen to his code if an error flag were set in the middle of that loop. (An infinite loop.)
Always check against good(), and if that fails, you can check to see which flag caused the failure: whether a benign (and wanted) eof() or an obnoxious bad() or fail() of some sort.
Ctrl-D is the EOF character on *nix and in ASCII (EOT).
Ctrl-Z is considered an EOF character on DOS.
However, all this is system dependent and you should not generally see these characters when doing file I/O --as EOF is technically a condition, not a part of a stream.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
1. No I thought it would overwrite it (like if you try to open a file, if it doesn't exist, it creates one)... guess it doesn't. Will look up file delete later (I funnily enough have a C++ tutorial now)
You're declaring an ofstream with the append flag (ios::app) which appends to the file, opens it and adds text instead of overwriting the content. Try it out without that flag, see what happens.
ofstream tempfile ("tempfile.txt", ios::app);
brechtjah
Junior Poster in Training
92 posts since Nov 2008
Reputation Points: 26
Solved Threads: 9