943,645 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 370
  • C++ RSS
Aug 14th, 2009
0

WritetoFile() order is messed

Expand Post »
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()

c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Aug 14th, 2009
0

Re: WritetoFile() order is messed

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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 14th, 2009
0

Re: WritetoFile() order is messed

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
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Aug 14th, 2009
0

Re: WritetoFile() order is messed

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...

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 22
Junior Poster
group256 is offline Offline
182 posts
since Apr 2009

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: Urgent Help Need!!
Next Thread in C++ Forum Timeline: CDialog - Assertion failure





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


Follow us on Twitter


© 2011 DaniWeb® LLC