• Print

Author Topic: Input & Current Line in File  (Read 179 times)

pitt

  • Full Member
  • ***
  • Posts: 243
  • Where's Timmy?
    • Email
Input & Current Line in File
« on: April 22, 2012, 06:58:09 PM »
I am curious to know if there is a way to tell which line you are currently on in a file using ...

OPEN filename$ for INPUT AS #1

... and then using INPUT z$ instead of LINE INPUT z$ due to the fact I'm working with CSV files and doing a LINE INPUT then parsing the line is not an option.

I have a counter (a = a + 1) for each INPUT, but that doesn't help when I need to know which line I am on in the text/csv file.
Where's Timmy?

Pete

  • Moderator
  • Hero Member
  • *****
  • Posts: 6296
  • Cuz I sez so varmint!
Re: Input & Current Line in File
« Reply #1 on: April 22, 2012, 07:15:18 PM »
I use LINE INPUT and just parse the commas.

If you used a binary read, you could identify the line number.

If you use just INPUT, then you cannot read the special characters, like chr$(10 and chr$(13) that let you know you are at the end of a file line.

Pete
It's only rocket science; it's not Linux!

Clippy

  • Hero Member
  • *****
  • Posts: 16446
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: Input & Current Line in File
« Reply #2 on: April 22, 2012, 07:25:49 PM »
WRITE starts a new line every WRITE. Every time you write to the file add the line number as the first value. You can WRITE multiple things at the same time.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

Pete

  • Moderator
  • Hero Member
  • *****
  • Posts: 6296
  • Cuz I sez so varmint!
Re: Input & Current Line in File
« Reply #3 on: April 22, 2012, 07:40:54 PM »
If you need to examine a CSV file, these methods will return the line number of the elements...


Code: [Select]
CLS
open "csvfile.csv" for output as #1
print #1, "a,b,c,d"
print #1, "e,f,g"
print #1, "h"
print #1, "i,j"
close #1

PRINT "Example #1, Binary Read": PRINT
dim q as string * 1: linenumber = 1
print linenumber;
open "csvfile.csv" for binary as #1
do until eof(1)
get #1,,q
if q = chr$(10) then
linenumber = linenumber + 1
print: print linenumber;
end if
if asc(q) > 31 then print q;
loop
close #1
linecount = 0
print: print

print "Example #2: Parse File": print
open "csvfile.csv" for input as #1
do until eof(1)
line input #1, a$
linecount = linecount + 1
print linecount;
do until instr(a$, ",") = 0
a1$ = mid$(a$, 1, instr(a$, ",") - 1)
print a1$;
a$ = mid$(a$, instr(a$, ",") + 1)
loop
if a$ <>"" then print a$ else print
loop
« Last Edit: April 23, 2012, 08:33:57 AM by Pete »
It's only rocket science; it's not Linux!

pitt

  • Full Member
  • ***
  • Posts: 243
  • Where's Timmy?
    • Email
Re: Input & Current Line in File
« Reply #4 on: April 23, 2012, 01:21:51 PM »
Quote from: Pete on April 22, 2012, 07:40:54 PM
If you need to examine a CSV file, these methods will return the line number of the elements...

Thanks!
Where's Timmy?

  • Print