944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 300
  • C++ RSS
Nov 2nd, 2009
0

Problem when generate ID from txt file

Expand Post »
I would like to automatically generated staff number before generated the staff number i would like to check the staff.txt is the staff number exist. If exist then it will get the last's staff number and continue to adding. But the code that i creating it wont work. Please help to me rewrite.

C++ Syntax (Toggle Plain Text)
  1. void staff::setstaffid()
  2. {
  3. int count = 0;
  4.  
  5. ifstream instaffile("staff.txt", ios::in);
  6.  
  7. if(!instaffile)
  8. {
  9. ofstream outstaffile("staff.txt", ios::out);
  10. outstaffile.close();
  11.  
  12. ifstream instaffile("staff.txt", ios::in);
  13. }
  14.  
  15. instaffile.read(reinterpret_cast<char *>(this), sizeof(staff));
  16.  
  17. if(instaffile.eof()) //if the staff id is 0 then it will start from 20000
  18. {
  19. staffid = 1;
  20. return;
  21. }
  22. else
  23. {
  24. while(instaffile && !instaffile.eof())
  25. {
  26. count++;
  27. instaffile.read(reinterpret_cast<char *>(this),sizeof(staff));
  28. }
  29. }
  30.  
  31. staffid = 1 + (count); //increment the staff id
  32.  
  33. instaffile.close();
  34. return;
  35. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
just4why is offline Offline
2 posts
since Nov 2009
Nov 3rd, 2009
-7
Re: Problem when generate ID from txt file
line 17 is probably the wrong way to determine if the file is empty or not. What you should do is seek to end of file then get the file position.
C++ Syntax (Toggle Plain Text)
  1. instaffile.seekg(0, ios::end);
  2. size_t sz = instaffile.tellg();
  3. if( sz == 0)
  4. {
  5. staffid = 1;
  6. return;
  7. }


The loop at lines 24-28 is also wrong
C++ Syntax (Toggle Plain Text)
  1. instaffile.seekp(0, ios::beg); // go to beginning of tile
  2. count = 0;
  3. while( instaffile.read(reinterpret_cast<char *>(this),sizeof(staff))
  4. {
  5. ++count;
  6. }
Last edited by Ancient Dragon; Nov 3rd, 2009 at 1:11 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 3rd, 2009
0
Re: Problem when generate ID from txt file
The code that you give me is not working. It is other method to write the code again without read the file from staff.txt?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
just4why is offline Offline
2 posts
since Nov 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: Computing factorial
Next Thread in C++ Forum Timeline: Getline and forloop/ While / Do while





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


Follow us on Twitter


© 2011 DaniWeb® LLC