forums

TNT Basic Forums > Programming Q&A
File Reading Question
< Last Thread     Next Thread >
Author
Thread        Post A Reply

07-05-2006 20:55

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Hello TNT Basic users,

Does any of you know how to count the lines in a "TEXT" file?
Is there any command like file length(fileid) that will do it? If not I'll make a request.

Thank you,
Jacob

07-05-2006 23:53

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

hehe... File Length (fileId) returns the number of chars but you can't read chars, only lines! usefull ^_^

File IO in TNT is really limited!

07-06-2006 10:13

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Hi Peoples...

Wow, not been here for what seems like ages!

Anywho... answers to the above. You can tell how many lines of text are in a file by counting the number of carriage returns (ASCII 13) or line feeds (ASCII 10) in the file (see Learning Zone - ASCII Codes Explained). You can't directly grab a single character from a file, which is a pain in the bum, but you can grab integers. Set 'newLines' to zero and divide the length of the file by four to get the number of complete integers, and use this in your outer loop. If '(fileLength/4)*4 <> fileLength' then inc newLines, as we can assume that one of the remaining chars is the final new line character. Grab the first four characters with File Read Int() and examine the characters in reverse order. To check char 4 use 'Bit And(my_int, 255)'. This masks out everything except the low order byte of the integer, the one we're interested in. To get at char 3 use 'Bit Shift(my_int, -8)' to shift it into position then mask as above. Repeat this for chars 2 and 1. Every time you find a 13 or a 10, add 1 to newLines.
A word of warning here: If the text file may have come from a Wintel PC you will need to check for a combination of carriage return and line feed together... You gotta love 'em!

A final note: Have you tried just reading a string until that string is NULL? Not on my Mac again so can't test, but it might work...

Have fun!

Danny (nods)

07-06-2006 14:55

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Danny, don't you think we should do something to make this easier? honestly not being able to grab chars (or bytes), having to do bit shifts, and all that stuff... just read your message, it's even much more complicated than in C/C++ with STL... this is BASIC, we can't ask beginner basic users to do bit shifts! i think this has got to be improved

07-07-2006 01:06

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

Yeah, there should be a command called something like Count File Lines(fileid).
However, I already solved the problem by making an ending line to the file. Because 'Set File Position fileid, pos' calls the line number, I run a for loop until I hit the ending line.

07-07-2006 07:50

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

I agree...

Not being able to grab bytes, or do anything with bytes in fact (coz we don't 'ave 'em!) can be a real pain, it's a throwback to the 'good old days' of STOS/AMOS.
While it looks complicated it's actually really quick, much quicker in fact than grabbing individual bytes from a device, as manipulating bits in RAM is what computers do best, but I fully agree that it may look a little bit (?!?) complex for a noob. Perhaps a couple of procedures in the Directory are called for as an interim fix until byte level access can be implemented. Some time ago I posted memory management calls to the Feature Request board on SourceForge, I didn't mention file access, but maybe I should have ;).
When I've finished my latest Wintel project I'll get back on the case and see what I can do...

Danny (nods)

07-07-2006 08:00

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

(an after thought)

If you're writing your own files you could always use the 'Pascal' approach and write the number of strings (as an int) to position 0 in the file and then grab strings in a loop based on this value.

Just a thought...

Danny (nods)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.