
Originally Posted by
Le_B
if i understand the problem correctly :
Then you don't undestand the problem correctly
the goal is not to read the whole content to get to n'th line. If you want last entry in the file you would need to read whole file using readLIne().
How you design your structure is up to you and depends on the data you have.
For the example given at the beggining simple structure with 4 ints is sufficient.
The most important part is to be able to tell where in the file your data is. If you want to get n'th record you need to be able to get pos = n*sizeof( struct ).
That implies that structure size can't be variable (no strings, vectors etc).
If you need variable size members then structure should contain member describing its size so you can jump around a file reading only size of the structure to be able to jump to the end of it and read another one, and so on and so forth.
If your files are huge and structures are variable length, then you may consider spliting it into two files, header and data blob. where data contains only data, header would be an array telling you where in the blob certan structure is:
//abstract
int structure_index = 630;
headerFile.seek( structure_index * sizeof( int ) );
int structure_position = 0;
headerFile >> strcuture_position;
dataFile.seek( structure_position );
dataFile >> structure;
//abstract
int structure_index = 630;
headerFile.seek( structure_index * sizeof( int ) );
int structure_position = 0;
headerFile >> strcuture_position;
dataFile.seek( structure_position );
dataFile >> structure;
To copy to clipboard, switch view to plain text mode
Bookmarks