Reading textfiles by line number...

Cableguy

Expert
Licensed User
Longtime User
We already have the ability to read text files (utf-8), and binary files...
Thoght binary files have positioning, its very dificult to know exactly were a datastream starts and ends, at least for me it is..

It would be of great help if we could read a specific line in a text file,
something like

FileReadLine(number)

and of course to get the number of lines a text file contains...

I know that, due to text wrapping, a file may apear to have more lines than it really has, so I'm aiming at the CHR(13) or crlf character as a end of line indicator...
 

Cableguy

Expert
Licensed User
Longtime User
Asssuming a text file is not that big, say 20 lines of text, is it practical to read all lines, line-by-line, to an array?
 

Cableguy

Expert
Licensed User
Longtime User
But won't that eat up a lot of device resources?
 

thedyg

Member
Licensed User
I'm still happy, if there is a function to readout or set the actual filepointer position.

My idea is, to scan a file (XML or whatever) one time sequentiel, find some keywords and save this to an array with the byteposition in the file.

Then i can save this array to another indexfile and reread it on the next appl. start very quickly back in my array.

If i need datas, i just search in the array the right keyword, readout the saved fileposition and move the filepointer to this position to readout all the detailed information in a very fast and easy way.

In Visual Basic there is a function SEEK available to readout or set this position.
http://msdn2.microsoft.com/en-us/library/7af2feyt(VS.71).aspx
 
Last edited:

dzt

Active Member
Licensed User
There are in b4ppc all the tools you need.

Open text file as binary file.
Write a function to read a whole line starting from a specific position and reading one byte at a time and when finds CRLF (or what ever) returns a string containing the whole line.
Keep the position of the first byte. That is your index
 

thedyg

Member
Licensed User
Hi Dimitri,

you've right.

But reading text in a byte per byte case normally is many slower then reading complete strings in one step.

It is also possible to read in line by line and count the string length plus 2 bytes for crlf to a separate counter. I think, thats the right workaround at the moment for me.

Here we are in the wishlist section :) It is just a wish of me to implement a function like seek in VB.

B.t.w: I'm very surprised about this dev environnment. It is easy, fast and very small ( in a view to a dev Studio from Microsoft where you need DVDs to install it). Just after a quarter hour of testing, i purchased a version of b4ppc !
 
Top