WritetoFile() order is messed

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

WritetoFile() order is messed

 
0
  #1
Aug 14th, 2009
Hi all

I have a function writetofile() which save a record to a file but the problem is when its writting to a file it doesnt write the record in a correct order, here is the code for WriteToFile()

  1. void StudentC::WriteToFile()
  2. {
  3. ofstream Outfile;
  4. Outfile.open("File\\Student.txt", ios::app);
  5.  
  6. Outfile << Count << endl;
  7. Outfile << StudentNo << endl;
  8. Outfile << Name << endl;
  9. Outfile << Surname << endl;
  10. Outfile << Gender << endl;
  11. Outfile << DateOfBirth << endl;
  12. Outfile << Mark << endl;
  13.  
  14. Outfile.Close();
  15. }

Now the problem is when it is saving a record to a file it 1st write the Count which is correct, from there it writes the Name instead of StudentNo, followed by Surname, DateofBirth, Gender, Mark and StudentNo, if u know why it is messing up my record please I need ur help urgent, coz the order should be like the way it is on the WriteToFile() method but it isnt, I hope this make sense

Thanks in Advance
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: WritetoFile() order is messed

 
0
  #2
Aug 14th, 2009
You're opening the file in append mode.
Are you sure you're not looking at the data from previous runs, where you might have had a different order to the data?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: WritetoFile() order is messed

 
0
  #3
Aug 14th, 2009
The append mode is just there to avoid the overwrite of the existing records

Im not sure if I understand what ur saying can u ellaborate a bit

Thanks
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: group256 is an unknown quantity at this point 
Solved Threads: 1
group256 group256 is offline Offline
Light Poster

Re: WritetoFile() order is messed

 
0
  #4
Aug 14th, 2009
Let me tell you something. You cut a part of your code and put it here, and when we try to figure out what's wrong, we are totally lost. hehehe, seriously!

But, first of all, it's "Outfile.close" not "Outfile.Close".

Second of all, There is nothing wrong with your code. But here are some pointers:

* Try to delete the Student.txt from your folder and run the program that I copied here and see the result.

* You explained why you used app, so beware of the outcome.

* You don't write your variables name like the way you do in human language. What I mean is, there are some conventions that you should follow. As far as I know, System Programmers and Unix programmers, use "_" kind of format, Like:

int pointer_to_file;

But Object oriented programmers and framework programmers use FIRST SMALL, REST BIG(I just made it up!!!). Means it's gonna be like:

int pointerToFile;

It's up to you how to use it, but I think you'd better at least follow a convention to give some professional look to your program. Professionals, please correct me if I'm wrong.

And after all the story here goes the code, just copy and paste it and compile...

  1. #include <fstream>
  2. using namespace std;
  3.  
  4.  
  5. void WriteToFile();
  6.  
  7.  
  8. int main()
  9. {
  10. WriteToFile();
  11.  
  12. return 0;
  13. }
  14.  
  15.  
  16. void WriteToFile()
  17. {
  18. int Count = 10, StudentNo = 102211;
  19. string Name = "Tom", Surname = "Hanks", Gender = "Male", DateOfBirth = "8/14/2009";
  20. double Mark = 15.5;
  21.  
  22. ofstream Outfile;
  23. Outfile.open("Student.txt", ios::app);
  24.  
  25. Outfile << Count << endl;
  26. Outfile << StudentNo << endl;
  27. Outfile << Name << endl;
  28. Outfile << Surname << endl;
  29. Outfile << Gender << endl;
  30. Outfile << DateOfBirth << endl;
  31. Outfile << Mark << endl;
  32.  
  33. Outfile.close();
  34. }
Last edited by group256; Aug 14th, 2009 at 11:46 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC