944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1095
  • C++ RSS
Jun 27th, 2009
0

How to count the number of objects in a file stream?

Expand Post »
I want to know that how many Student class objects in a file.
plz help me out.
Below is a class of Students and 2 function for read and write object in a file, in read function
C++ Syntax (Toggle Plain Text)
  1. while(in.eof()!= 1){
  2. in.read((char *)&st,sizeof(st));
  3. st.showData();
  4. }
st.showData(); always show the last object twise.. unable to sort out my mistake in the code.

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <fstream>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. class Student{
  8. private:
  9. char name[30];
  10. int id;
  11. int rollNo;
  12.  
  13. public:
  14. Student(){}
  15. Student(char *n,int i, int r){
  16. strcpy_s(name,n);
  17. id = i;
  18. rollNo = r;
  19. }
  20. void getData(){
  21. cout << "Enter Name : "; cin >> name;
  22. cout << "Enter Id : "; cin >> id;
  23. cout << "Enter Roll No : "; cin>> rollNo;
  24. }
  25.  
  26. void showData(){
  27. cout << "Student Detail---------------"<<endl;
  28. cout << "Name : " << name << endl;
  29. cout << "Id Number : " <<id << endl;
  30. cout << "Roll No : " <<rollNo << endl;
  31. }
  32. };
  33.  
  34. void outData(Student &st){
  35. ofstream out;
  36. out.open("obj.txt",ios::app | ios::binary);
  37. out.write((char *)&st,sizeof(st));
  38. out.close();
  39. }
  40.  
  41. void inData(Student st){
  42. int count =0;
  43. ifstream in;
  44. in.open("obj.txt",ios::in|ios::binary);
  45. while(in.eof()!= 1){
  46. in.read((char *)&st,sizeof(st));
  47. st.showData();
  48. }
  49.  
  50. in.close();
  51.  
  52. }
  53. int main()
  54. {
  55.  
  56. Student st;
  57. inData(st);
  58.  
  59. return 0;
  60. }
yun
Reputation Points: 10
Solved Threads: 2
Light Poster
yun is offline Offline
42 posts
since May 2009
Jun 27th, 2009
3

Re: How to count the number of objects in a file stream?

This: while(in.eof()!= 1)
is probably why it is always shows the last object read in twice. You shouldn't use the return value of eof() to control performance of a loop body for that very reason. Lot's of folks have bookmarked a reference to a technical explanation for why that's so, but I've never really understood it all that well, so I won't try to explain it. I just know this type of problem can develop when you use that type of approach. To try to fix it, try this:
C++ Syntax (Toggle Plain Text)
  1. while(in.read((char *)&st,sizeof(st)))
  2. {
  3. st.showData();
  4. }
Last edited by Lerner; Jun 27th, 2009 at 10:41 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jun 27th, 2009
0

Re: How to count the number of objects in a file stream?

Well how have you written the records in the file from which you are taking the input ?
Extras :
>Try not using .eof() function its not good to use it like this several times discussed already in this forum.
>And within the read looping you can always include a counter right to know how much you have read.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jun 28th, 2009
0

Re: How to count the number of objects in a file stream?

Find the length of file. Divide the length of file by size of an object.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Seriously? Windows Winnie Tute
Next Thread in C++ Forum Timeline: Copy constructor , Local Variable ,Destructor





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


Follow us on Twitter


© 2011 DaniWeb® LLC