Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can Qbasic read the amount of lines in a file?
#1
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 helpSmileSmileSmile
quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Reply
#2
UNTESTED!!!

Code:
lines=0
do
Lineinput filenum, A$
lines=lines+1
Loop until eof(Filenum)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#3
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... Wink
Reply
#4
thank you thank you!
quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Reply
#5
Wait, nevermind

Sorry, I was in a hurry and did not test the code Sad

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]
Reply
#6
Problem solved on MSN...

Just confused the variable type definition...

#variable = variable

But

variable# does not = variable
Reply
#7
Yes, thank you =)
quote="na_th_an"]
Greenday, Spice Girls... Can you tell the difference?
[/quote]
Reply
#8
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.
Reply
#9
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.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)