locked
counting iteams in a file RRS feed

  • Question

  • I am trying to make a program that takes in user inputs(just numbers), stores those values in a file, tallies the values, and displays the counts.

    I've gotten that far, but the list to input Is rather large and will take many weeks to input it all.

    so I was wondering if there was a way to read and recount all values in the file every time I restart the program.

    thanks,

    james

    Sunday, February 9, 2014 11:23 PM

Answers

  • You should look at the use of the File methods, especially File.ReadLine and File.ReadContents.

    Jan [ WhTurner ] The Netherlands

    • Marked as answer by james e ortega Monday, February 10, 2014 10:29 PM
    Monday, February 10, 2014 10:05 AM
    Answerer
  • Unfortunately I don't think there is a way to detect the end of a file read in Small Basic using File.ReadLine without using an extension.

    There is no point in doing the 'File.ReadLine(newFile,i)' line, if you immediately read the same line again in the If statement (won't cause a problem, just unnecessary and may slow it a bit).

    Recommend to store the number of lines (i.e number of values) in the file as the first line.

    Note that Small Basic will slow considerably if you start to use arrays with more than a few hundred (say 200) elements.

    • Marked as answer by james e ortega Tuesday, February 11, 2014 12:28 AM
    Monday, February 10, 2014 10:38 PM

All replies

  • You should look at the use of the File methods, especially File.ReadLine and File.ReadContents.

    Jan [ WhTurner ] The Netherlands

    • Marked as answer by james e ortega Monday, February 10, 2014 10:29 PM
    Monday, February 10, 2014 10:05 AM
    Answerer
  • thank you for replying.

    I got it to work with the File.ReadLine.

    here is my sample:

    Sub initializeTallyCount
      For i = 1 To 10000
        File.ReadLine(newFile,i)
        If(File.ReadLine(newFile,i) = 1)Then
          one = one + 1
          EndIf
        EndFor
    EndSub

    my main problem with this is I don't know how to set the for loop end count. I put in 10000 to make sure it was large enough to count each item. but I would like it better if I could put a variable at the end count and set that variable to make sure it went through each line in the file.

    thanks,

    james

    Monday, February 10, 2014 10:29 PM
  • Unfortunately I don't think there is a way to detect the end of a file read in Small Basic using File.ReadLine without using an extension.

    There is no point in doing the 'File.ReadLine(newFile,i)' line, if you immediately read the same line again in the If statement (won't cause a problem, just unnecessary and may slow it a bit).

    Recommend to store the number of lines (i.e number of values) in the file as the first line.

    Note that Small Basic will slow considerably if you start to use arrays with more than a few hundred (say 200) elements.

    • Marked as answer by james e ortega Tuesday, February 11, 2014 12:28 AM
    Monday, February 10, 2014 10:38 PM
  • awesome insight!

    "Recommend to store the number of lines (i.e number of values) in the file as the first line."

    That is perfect!

    thank you.

    Tuesday, February 11, 2014 12:35 AM
  • Another possibility is (if there are no blank lines in the file) to use:

    value=File.Readline(Nedfile,i)

    if value="" then   'found empty line or end of file

      i=99999  ' bigger then end: stop the for-loop

    endif


    Jan [ WhTurner ] The Netherlands

    Tuesday, February 11, 2014 9:30 AM
    Answerer