can i read and write to a particular location in a text file rather than reading every line in one at a time? the goal is to use the text file as a database and not have it slow down as the file gets bigger? :)
Printable View
can i read and write to a particular location in a text file rather than reading every line in one at a time? the goal is to use the text file as a database and not have it slow down as the file gets bigger? :)
Its not so hard.
If you want to read from a specific line.
http://www.cplusplus.com/doc/tutorial/files.html that might help you.
Actually, it's impossible. You can't just jump to the start of any line in a text file, you have to scan it linearly.
Text files do not make good databases.
well i am getting into that tutorial today iron skull. :)
cornedbee, well i don't need a relational database, that would be wastefull for my purposes, i just need to store single field records of variable length. ;)
in the header of the file i was hoping to record the name, location and size of each record an index if you will, and then be able to move down the file to read the record without having to read in every line. Surely there must be an easy way to do it?
The C++ standard says that the only valid values for fstream::seekg() for text files are those previously returned by tellg(). I'm not sure if pos_t is serializable - if it is, then you can create an index (but it must be external to your main file), if not, then you can't.
That's not to say that it won't work on your platform, but it isn't defined according to the standard.
...
OK, looked up fpos. There is no requirement that the type is streamable, therefore one must assume that it isn't.
or maybe when you store/write it to a file, write in ascending order and then use binary search to find it. i dont know if it would be any help or not.
Binary search requires random access, which you don't have.
hmmm what do you mean random access CB? sorry newbie.
It means you can retrieve the data from the file, for example, using an index, directly.Quote:
Originally Posted by ayahnabunda
..CornedBee has already explained that to get to a position in a file, you have to scan it linearly, which means from beginning to end. Thus, unable to randomly access anything without a linear scan beforehand.Code:file[2]; // gets the 3rd byte in the file
chem