Reading textfiles by line number...

Discussion in 'Basic4ppc Wishlist' started by Cableguy, Aug 6, 2007.

  1. Cableguy

    Cableguy Expert Licensed 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


    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...
  2. Erel

    Erel Administrator Staff Member Licensed User

    This is technically impossible without first reading (number-1) lines.
    Each line can be with a different length (characters and bytes) so without some kind of index (which regular text files do not include) you can't jump to a specific line.
  3. Cableguy

    Cableguy Expert Licensed 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?
  4. Erel

    Erel Administrator Staff Member Licensed User

    A much larger file can also be loaded into an array.
  5. Cableguy

    Cableguy Expert Licensed User

    But won't that eat up a lot of device resources?
  6. Erel

    Erel Administrator Staff Member Licensed User

    Each character takes about 2 bytes.
    So 100 lines of text * 50 characters = 10kb.
    This is not too much memory. A single bitmap takes more than that.
  7. thedyg

    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.
    Last edited: Aug 7, 2007
  8. dzt

    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
  9. thedyg

    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 !
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice