Counting the number of objects in a file

Suppose, I have written number of objects in a file.Each object contain different information. Those objects belong to different classes.
Now before reading, I want to calculate number of objects in file for some purpose.
How can i do that?
You cannot. Only by reading you will know the type of current object. Only by reading you will know the size of current object and thus where the next object will be found.

Unless you wrote out to file the object count.
Actually problem is:

I have written some objects in file.
There is a base class and other are derived from that.
Objects either belong to base class or any of the derived classes.
All those objects are pointed by an array of pointers of the type of base class.
Next time i open the file in append mode.Now i want that next object i enter should be stored in the next element of array.
I can only do this if i know initial number of objects in file.
Is there any way to do that?

How I did this was to have a header ( structure of data ) that is written at the beginning of the file with information I needed to store such as size of data, position in file for areas of the data and then the actual data.

Reading in that header data first would then give you the required information.
So we need to give information of size of all the types of objects.That is okay,but..
I am not getting your point of storing position in file for areas of data.
How can we know at the beginning of file that which type of data will be entered at a particular point.?
And how can we count the number of objects using it. Please elaborate.
1
2
3
4
5
6
7
8
void foo( ostream & fout, const vector<Bar> & bar )
{
  fout << bar.size(); // we know what we will do in the loop below
  for ( auto b : bar )
  {
    fout << b;
  }
}

Assuming the file is stored in this layout:

|---- Header----|----Student Data----|----Course Data---|----Achievements----|

Position is x byte position in file so you could seek to that position and load the data, and having the number of objects also in the header you know how many to read in.

So the header could have:

ID (some random ascii to identify it and for you to check against)
Size of student structure
Size of course structure
Size of achievements structure.
Position of student structures (offset from beginning of file)
Number of student structures stored (how many objects)
Position of course structures (offset from beginning of file)
Number of course structures stored (how many objects)
Position of achievements structures (offset from beginning of file)
Number of achievements structures stored (how many objects)

.. and any other information you may like to store, i had timestamp data for last modified, and who modified (assuming you have a login system to track that).

Since your designing the header your in full control and can store whatever you like. :)



The offset in the header is the start position of the first object of that type, and by knowing how many objects (from the header file) you could read them in easily.

Thankyou. It was very helpful.
Your welcome.
Topic archived. No new replies allowed.