Link to home
Start Free TrialLog in
Avatar of kkarnez
kkarnez

asked on

Text File and Seek...

Hi to everyone
Seek doesn't work with text files...How can I solve this problem?
(I use the native in/out routines)....

Thanks
Avatar of jlove1
jlove1

I suggest defining a filetype of FILE, and using blockread, and seek to find what your wanting. You COULD use the existing variable (and convert to FILE type), or you could simply make a new file variable and assign it to the same file. Hope this helps.

The other method would be to RESET the file, then do a for loop to the number where you want to seek into like this.
for a := 1 to seek do read(f)

 
Avatar of kkarnez

ASKER

If I use the first method then I will not be able to read with native routines.
The second method is very simple but the reason I want to 'Seek' is for  speed and this doesn't help.

I'm sorry, but you'll either have to use some NON-NATIVE routines, or use some slow routine.. Unless you're always "seeking ahead"....

Well, I just had another idea.. It'll only work if your text file is 64K or less.. I was going to post some code where you could load the entire file into memory as an array (pointer to that is) you could then perform all functions on that array... That;s about all I can come up with and still be "native"..

I really don't think there is a way to Natively seek into a text file.
sorry :-(

ASKER CERTIFIED SOLUTION
Avatar of Stuart_Johnson
Stuart_Johnson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This may work in Delphi, but not in BP/TP.. I just tested it :-(..

JLove,

I presume you where directing your last comment at me.  What has B/T Pascal got to do with anything?  This is a Delphi area.  There was no mention in the question of this being a Pascal question (not that I can see anyway).

Stu.
Avatar of kkarnez

ASKER

I guess there isn't a way to implement it with native routines...
Im sorry, I dont understand what you mean by "native routines"?  Can you ellaborate on this at all?  Do you mean like a direct API call or something?

Stu
Avatar of kkarnez

ASKER

I mean pascal native routines, like Read and Write and not OpenRead for example
When you are doing a seek into a file, you need to be able to specify where you want to seek too.  In a text file, there are no records, or index markers, which enable a seek to work.

Another way you could do it would be to use the BlockRead function, which has been used since the early days of Pascal.  You could open the text file, and use BlockRead to read into the file by a set length, ie

BlockRead(TFile, Buffer, SeekSize, ReadResult);

Obviously, you would not be able to read in more data the the buffer would allow, but you could do it in a loop until you got to the section you require.

Another way you could try it is to open the file as an untyped file (ie: var TFile: File) and then you should be able to use the seek command.  I havent used Pascal for a while, or the Assign, Reset, Rewrite etc commands for ages, so I can't remember exactly.

Anyway, hope this helps.

Stu.