| | |
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:
Solved Threads: 2
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
st.showData(); always show the last object twise.. unable to sort out my mistake in the code.
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)
while(in.eof()!= 1){ in.read((char *)&st,sizeof(st)); st.showData(); }
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include <fstream> #include <string.h> using namespace std; class Student{ private: char name[30]; int id; int rollNo; public: Student(){} Student(char *n,int i, int r){ strcpy_s(name,n); id = i; rollNo = r; } void getData(){ cout << "Enter Name : "; cin >> name; cout << "Enter Id : "; cin >> id; cout << "Enter Roll No : "; cin>> rollNo; } void showData(){ cout << "Student Detail---------------"<<endl; cout << "Name : " << name << endl; cout << "Id Number : " <<id << endl; cout << "Roll No : " <<rollNo << endl; } }; void outData(Student &st){ ofstream out; out.open("obj.txt",ios::app | ios::binary); out.write((char *)&st,sizeof(st)); out.close(); } void inData(Student st){ int count =0; ifstream in; in.open("obj.txt",ios::in|ios::binary); while(in.eof()!= 1){ in.read((char *)&st,sizeof(st)); st.showData(); } in.close(); } int main() { Student st; inData(st); return 0; }
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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:
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)
while(in.read((char *)&st,sizeof(st))) { st.showData(); }
Last edited by Lerner; Jun 27th, 2009 at 10:41 pm.
Klatu Barada Nikto
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.
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"....
![]() |
Similar Threads
- how to count number of times file downloaded from my website (ASP.NET)
- count number of occurrences of chars in a file (C++)
- can i limit the number of lines read in from a file? (C++)
- how to count paragraphs from a text file. (C)
- how to count the no of pages in a pdf file using java script (JavaScript / DHTML / AJAX)
- file stream. where do .dat files go? (C++)
- Skip initial 4 bytes in a binary stream (C++)
- Count number of occurrences of a character search it in a file (C++)
Other Threads in the C++ Forum
- Previous Thread: Seriously? Windows Winnie Tute
- Next Thread: Copy constructor , Local Variable ,Destructor
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






