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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 37
Reputation: yun is an unknown quantity at this point 
Solved Threads: 2
yun yun is offline Offline
Light Poster

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

 
0
  #1
Jun 27th, 2009
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
  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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
3
  #2
Jun 27th, 2009
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:
  1. while(in.read((char *)&st,sizeof(st)))
  2. {
  3. st.showData();
  4. }
Last edited by Lerner; Jun 27th, 2009 at 10:41 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

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

 
0
  #3
Jun 27th, 2009
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.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

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

 
0
  #4
Jun 28th, 2009
Find the length of file. Divide the length of file by size of an object.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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