943,960 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1010
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 18th, 2008
0

Reading a file. Copying over to other file. Renaming. Renaming = -1?

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. void writingTransactionToFile(string transaction)
  2. {
  3. string temp;
  4. int test;
  5. ifstream myfile("transactionlog.txt");
  6. ofstream tempfile ("tempfile.txt", ios::app);
  7. if (tempfile.is_open())
  8. {
  9. tempfile << transaction;
  10. if(myfile.is_open())
  11. {
  12. while(!myfile.eof())
  13. {
  14. getline(myfile,temp);
  15. cout << temp;
  16. tempfile << temp << endl;
  17. }
  18. } else {
  19. cout << "Unable to open file.";
  20. }
  21. tempfile.close();
  22. myfile.close();
  23. } else
  24. cout << "Unable to open file";
  25. test = rename("tempfile.txt", "transactionlog.txt");
  26. if(test != 0)
  27. perror("Error: ");
  28. cout << test;
  29. }

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?
Similar Threads
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 18th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

call me silly but why not just send the data straight to the correct file name. Also i would recomend you do all file copying in binary mode to prevent loss of any data.

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 18th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

call me silly but why not just send the data straight to the correct file name. Also i would recomend you do all file copying in binary mode to prevent loss of any data.

Chris
Because I want the latest data to be at the top and this is the only way to do it.
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 19th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

What exactly do you mean by 'at the top'?

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 19th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

What exactly do you mean by 'at the top'?

Chris
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' )
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 19th, 2008
1

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

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:
C++ Syntax (Toggle Plain Text)
  1. while(!myfile.eof())
  2. {
  3. getline(myfile,temp);
  4. cout << temp;
  5. tempfile << temp << endl;
  6. }

to this:
C++ Syntax (Toggle Plain Text)
  1. while(getline(myfile,temp))
  2. {
  3. cout << temp;
  4. tempfile << temp << endl;
  5. }
The eof() function is gone, and it also looks a lot cleaner
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 19th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -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)
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 19th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

Ok realising I am retarded. I should have taken the 'Error Message' literally. It kept saying 'File Exists', which I then thought yes, it does exist, so rename it to that! Raging at my laptop.

However it now works. Thanks.
Reputation Points: 11
Solved Threads: 1
Light Poster
Lokolo is offline Offline
29 posts
since Nov 2008
Nov 20th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

Click to Expand / Collapse  Quote originally posted by Lokolo ...
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)
EOF can return true for more than that reason i.e certain characters can cause it to return true. I believe '\v' is one of those. This is why it should be avoided.

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 20th, 2008
0

Re: Reading a file. Copying over to other file. Renaming. Renaming = -1?

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: does anyone know what this means?
Next Thread in C++ Forum Timeline: help :S





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC