locked
How to sort the number of line of a .txt file RRS feed

  • Question

  • I everybody.

    o make some management of save file in .txt format i need to now the number of line in a .txt file to transform the file in an array variable . The [i] value of the array being the value of each line of the .txt file.

    Does any sub routine of smallbasic doing this ?

    Best Regards

    benoit406

    Sunday, March 4, 2012 8:56 AM

Answers

  • You can read each line into an array using the following.  Note that you need to know the number of lines in the file - perhaps the first line of the file says how many lines are in the file or the last line of the file is blank or has a special keyword like "END".

    FileName = Program.Directory+"\test.txt"
    For i = 1 To 10
      data[i] = File.ReadLine(FileName,i)
    EndFor
    For i = 1 To Array.GetItemCount(data)
      TextWindow.WriteLine(data[i])
    EndFor

    Sunday, March 4, 2012 9:50 AM
  • litdev,

    I would like to propose a While loop solution to the problem.  I have been tinkering with a console database management program that uses Comma Separated Values to store data, with one record of data per line (the commas separate the fields).

    endOfFile = "False"
      While endOfFile = "False"
        lineIn = File.ReadLine(workingFile, recordNumber)
        If lineIn <> "" Then 
          ParseString()
          recordNumber = recordNumber + 1
          numRecords = numRecords + 1
        Else
          endOfFile = "True"
        EndIf
      EndWhile

    In my example, if nothing is encountered during a line read, then it assumes an End Of File has been reached.

    ParseString() handles removing the commas and inserting the data into an array of records (with recordNumber as the first dimension, also used as the line number during Read).  The numRecords variable counts how many records there are for display purposes (used globally and gets modified as I add/subtract records).

    Line zero in the workingFile holds the names of the fields (separated by commas), with all subsequent lines containing record data.

    Sunday, March 4, 2012 5:54 PM
  • kam528,

    Hi your answer is good using a while loop, the only problem is there is no definite way in SmallBasic to identify the end of a file: we can assume an empty line or something else, but cannot be certain.  The while loop is certainly the best way in general, with the only exception that we need to be sure it will not go on for ever (never find a terminating code e.g. "END") or terminate too early (e.g. a blank line in the middle of the file).

    There are extensions that can detect the end of a file or just read all contents into an array directly, but better to learn by coding it where you can in SmallBasic.

    I guess it all depends on the format of the file and how certain we are that the format will always be used.

    Sunday, March 4, 2012 7:54 PM

All replies

  • You can read each line into an array using the following.  Note that you need to know the number of lines in the file - perhaps the first line of the file says how many lines are in the file or the last line of the file is blank or has a special keyword like "END".

    FileName = Program.Directory+"\test.txt"
    For i = 1 To 10
      data[i] = File.ReadLine(FileName,i)
    EndFor
    For i = 1 To Array.GetItemCount(data)
      TextWindow.WriteLine(data[i])
    EndFor

    Sunday, March 4, 2012 9:50 AM
  • Thanks for the answer.

    It was one of my solution to make the line 1 "the number of written  line" in the .txt file.

    Now i will work on it. ;-)

    Sunday, March 4, 2012 11:21 AM
  • litdev,

    I would like to propose a While loop solution to the problem.  I have been tinkering with a console database management program that uses Comma Separated Values to store data, with one record of data per line (the commas separate the fields).

    endOfFile = "False"
      While endOfFile = "False"
        lineIn = File.ReadLine(workingFile, recordNumber)
        If lineIn <> "" Then 
          ParseString()
          recordNumber = recordNumber + 1
          numRecords = numRecords + 1
        Else
          endOfFile = "True"
        EndIf
      EndWhile

    In my example, if nothing is encountered during a line read, then it assumes an End Of File has been reached.

    ParseString() handles removing the commas and inserting the data into an array of records (with recordNumber as the first dimension, also used as the line number during Read).  The numRecords variable counts how many records there are for display purposes (used globally and gets modified as I add/subtract records).

    Line zero in the workingFile holds the names of the fields (separated by commas), with all subsequent lines containing record data.

    Sunday, March 4, 2012 5:54 PM
  • kam528,

    Hi your answer is good using a while loop, the only problem is there is no definite way in SmallBasic to identify the end of a file: we can assume an empty line or something else, but cannot be certain.  The while loop is certainly the best way in general, with the only exception that we need to be sure it will not go on for ever (never find a terminating code e.g. "END") or terminate too early (e.g. a blank line in the middle of the file).

    There are extensions that can detect the end of a file or just read all contents into an array directly, but better to learn by coding it where you can in SmallBasic.

    I guess it all depends on the format of the file and how certain we are that the format will always be used.

    Sunday, March 4, 2012 7:54 PM