Author Topic: How to process each line in a file?  (Read 226 times)

sst001

  • Newbie
  • *
  • Posts: 5
How to process each line in a file?
« on: July 08, 2019, 10:12:11 am »
Hi guys,

I have a .txt file full of single line words, and phrases. For example, a .txt file with 20 lines, each line = 1 to 5 words.

I would like to send each line through some processing via a gosub routine until EOF.

Here is what I'm using:

Code: [Select]
;Read through file line by line
termfile = "c:\kde\searchterms.txt"
handle = FileOpen(termfile, "READ")
count = 0
While @TRUE ; Loop till break do us end
   line = FileRead(handle)
   If line == "*EOF*" Then Break
   count = count + 1
term = StrCat("",line)
gosub process_term
EndWhile
FileClose(handle)

Everything works fine until the gosub process_term return which errors out at 'line=FileRead (handle)'. So when the gosub is returned for the next line in the file, it bombs. I'm thinking I need a for loop under "count = count + 1"?

How do I send each line from termfile down gosub process_term for processing, and then when EOF hits processing stops?

Hope this enough info.

Thanks!
ST

td

  • Tech Support
  • *****
  • Posts: 3099
    • WinBatch
Re: How to process each line in a file?
« Reply #1 on: July 08, 2019, 10:24:46 am »
It is impossible to answer your question without knowing the exact error you are receiving and the content of your subroutine.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

sst001

  • Newbie
  • *
  • Posts: 5
Re: How to process each line in a file?
« Reply #2 on: July 09, 2019, 06:33:02 am »
Thanks, this solved it for me:

Code: [Select]
filename = "c:\test\test.txt"
file = FileGet(filename)
file = StrReplace(file,@CRLF,@TAB)
llinecount = ItemCount(file,@TAB)

For ii = 1 To linecount

  line = ItemExtract(ii,file,@TAB)

  ; You would do your stuff here

Next

Exit

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 276
Re: How to process each line in a file?
« Reply #3 on: July 09, 2019, 07:01:08 pm »
I guess all's well that ends well.

td

  • Tech Support
  • *****
  • Posts: 3099
    • WinBatch
Re: How to process each line in a file?
« Reply #4 on: July 10, 2019, 06:40:06 am »
Something involving a reference to sleeping dogs might be appropriate here.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade