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: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

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

 
0
  #1
Nov 18th, 2008
  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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

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

 
0
  #2
Nov 18th, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

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

 
0
  #3
Nov 18th, 2008
Originally Posted by Freaky_Chris View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

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

 
0
  #4
Nov 19th, 2008
What exactly do you mean by 'at the top'?

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

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

 
0
  #5
Nov 19th, 2008
Originally Posted by Freaky_Chris View Post
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' )
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,819
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

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

 
1
  #6
Nov 19th, 2008
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:
  1. while(!myfile.eof())
  2. {
  3. getline(myfile,temp);
  4. cout << temp;
  5. tempfile << temp << endl;
  6. }

to this:
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

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

 
0
  #7
Nov 19th, 2008
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)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Lokolo is an unknown quantity at this point 
Solved Threads: 1
Lokolo Lokolo is offline Offline
Light Poster

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

 
0
  #8
Nov 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

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

 
0
  #9
Nov 20th, 2008
Originally Posted by Lokolo View Post
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #10
Nov 20th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC