| | |
Reading a file. Copying over to other file. Renaming. Renaming = -1?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
void writingTransactionToFile(string transaction) { string temp; int test; ifstream myfile("transactionlog.txt"); ofstream tempfile ("tempfile.txt", ios::app); if (tempfile.is_open()) { tempfile << transaction; if(myfile.is_open()) { while(!myfile.eof()) { getline(myfile,temp); cout << temp; tempfile << temp << endl; } } else { cout << "Unable to open file."; } tempfile.close(); myfile.close(); } else cout << "Unable to open file"; test = rename("tempfile.txt", "transactionlog.txt"); if(test != 0) perror("Error: "); cout << test; }
Ok so its mainly the rename function that doesn't work. It just returns the value -1. perror doesn't return any error.
Not sure why it doesn't like it?
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
Because I want the latest data to be at the top and this is the only way to do it.
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
At the top of the text file.
i.e. its a list of transactions and I want the most recent transactions at the top. At the bottom of the file is the first transaction.
My text file would look something like this:
17/11/2008 - Something happened
14/11/2008 - Something happened before
11/11/2008 - etc.
10/11/2008 - etc.
So when I then print the stuff from the text file on screen it looks like this.
The only otherway I could think of is to read the text file backwards - but I have no idea how to do this either and cannot find a way of doing it. (whereas the method above seems to work, but the rename bit isn't, even though the error returns 'File Exists'
)
i.e. its a list of transactions and I want the most recent transactions at the top. At the bottom of the file is the first transaction.
My text file would look something like this:
17/11/2008 - Something happened
14/11/2008 - Something happened before
11/11/2008 - etc.
10/11/2008 - etc.
So when I then print the stuff from the text file on screen it looks like this.
The only otherway I could think of is to read the text file backwards - but I have no idea how to do this either and cannot find a way of doing it. (whereas the method above seems to work, but the rename bit isn't, even though the error returns 'File Exists'
) 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:
to this:
The eof() function is gone, and it also looks a lot cleaner
You should also avoid using eof().
If you change this:
C++ Syntax (Toggle Plain Text)
while(!myfile.eof()) { getline(myfile,temp); cout << temp; tempfile << temp << endl; }
to this:
C++ Syntax (Toggle Plain Text)
while(getline(myfile,temp)) { cout << temp; tempfile << temp << endl; }
•
•
Join Date: Nov 2008
Posts: 29
Reputation:
Solved Threads: 1
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)
2. Ok will change code to that, not much difference - doesn't eof check if there is a line available? So its just doing the same (I could be wrong)
2. Ok will change code to that, not much difference - doesn't eof check if there is a line available? So its just doing the same (I could be wrong)
•
•
•
•
2. Ok will change code to that, not much difference - doesn't eof check if there is a line available? So its just doing the same (I could be wrong)
Chris
Knowledge is power -- But experience is everything
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.
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: does anyone know what this means?
- Next Thread: help :S
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






