Posts: 548
Threads: 67
Joined: Jun 2004
Ok, I'm trying to open up a file and determine how many lines long it is using Qbasic. How do I do this?
thanks for help
quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Posts: 3,288
Threads: 167
Joined: Nov 2001
UNTESTED!!!
Code:
lines=0
do
Lineinput filenum, A$
lines=lines+1
Loop until eof(Filenum)
Posts: 1,845
Threads: 44
Joined: Aug 2002
Maybe you want:
[syntax="qbasic"]Lines% = 0
DO WHILE NOT EOF(FileNum)
LINE INPUT FileNum, A$
Lines% = Lines% + 1
LOOP[/syntax]
I think your code will either return an error or Lines = 1 on a zero-length file...
Posts: 548
Threads: 67
Joined: Jun 2004
thank you thank you!
quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Posts: 548
Threads: 67
Joined: Jun 2004
Wait, nevermind
Sorry, I was in a hurry and did not test the code
The syntax for line input would be LINE INPUT #1, a$
But the syntax for EOF(1).
LINE INPUT needs to have a #, but EOF needs to have an integer with out a number. So you guys got a different solution? Thanks.
quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Posts: 3,522
Threads: 189
Joined: Dec 2003
Problem solved on MSN...
Just confused the variable type definition...
#variable = variable
But
variable# does not = variable
Posts: 548
Threads: 67
Joined: Jun 2004
Yes, thank you =)
quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Posts: 3,368
Threads: 195
Joined: Jan 2003
If it's small enough (less than 65 (?) KB ) you could try this. Which may or may not be massively slower or faster.
Code:
'Untested
blahtxt$ = "blah.txt"
OPEN blahtxt$ FOR BINARY AS #1 'or random?
lof1% = LOF(1) 'get the length of the file
a$ = STRING$(lof1%, " ") 'make a string the size of the file
GET #1, a$ 'this should load in the file to the string
a$ = a$ + " " 'this is so the program won't crash if there's an enter character at the end. (it checks the last ENTER plus one, so it wouldn't exist would it!.
enterExists% = 0
DO
enterExists% = INSTR(enterExists% + 1, a$, CHR$(13))
if enterExists% = 0 then EXIT DO
lineAmount% = lineAmount% + 1
LOOP
CLOSE #1
PRINT lineAmount%; "lines in "; blahtxt$
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,956
Threads: 65
Joined: Jun 2003
AGA, 'ol buddy, where have you been? I missed your complicated logic, like the one you just posted for counting lines in a file. What's wrong with a straightforward approach like reading the file using LINE INPUT to the end and counting records (lines) on the way?
Oh, BTW, your checking for a chr$(13) may not always work. A line or record ends in a Carriage Return and a Line Feed which is chr$(13) + chr$(10). It is conceivable that you could have a legitimate record with one or more Carriage Returns embeded in it.
Depending on how the file was created (by program, editor, word processor, etc.) the LAST RECORD may sometimes not end in a Carriage Return Line Feed. The little program that reads the file until EOF will still detect the last record without the CRLF because it reads until the end of the file, not until the last CRLF.
There are other considerations which make the CRLF test not foolproof, but I'm tired and you probably don't care.
*****