Problem when generate ID from txt file

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

Join Date: Nov 2009
Posts: 2
Reputation: just4why is an unknown quantity at this point 
Solved Threads: 0
just4why just4why is offline Offline
Newbie Poster

Problem when generate ID from txt file

 
0
  #1
29 Days Ago
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
29 Days Ago
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.
  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
  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; 29 Days Ago at 1:11 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: just4why is an unknown quantity at this point 
Solved Threads: 0
just4why just4why is offline Offline
Newbie Poster
 
0
  #3
29 Days Ago
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?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC