|
-
Mar 27th, 2007, 02:30 AM
#1
Thread Starter
Banned
direct access to a location in a text file
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?
-
Mar 27th, 2007, 04:15 AM
#2
Hyperactive Member
Re: direct access to a location in a text file
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.
506C65617365205261746520506F7374732E2E2E
-
Mar 27th, 2007, 06:09 AM
#3
Re: direct access to a location in a text file
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 27th, 2007, 06:17 PM
#4
Thread Starter
Banned
Re: direct access to a location in a text file
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?
-
Mar 27th, 2007, 07:53 PM
#5
Re: direct access to a location in a text file
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 29th, 2007, 06:11 PM
#6
Addicted Member
Re: direct access to a location in a text file
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.
effort effort effort and still effort
-
Mar 29th, 2007, 06:13 PM
#7
Re: direct access to a location in a text file
Binary search requires random access, which you don't have.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 29th, 2007, 08:17 PM
#8
Addicted Member
Re: direct access to a location in a text file
hmmm what do you mean random access CB? sorry newbie.
effort effort effort and still effort
-
Mar 30th, 2007, 12:23 AM
#9
Re: direct access to a location in a text file
 Originally Posted by ayahnabunda
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.
Code:
file[2]; // gets the 3rd byte in the file
..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.
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|