Department of Energy Argonne National Laboratory Office of Science NEWTON's Homepage NEWTON's Homepage
NEWTON, Ask A Scientist!
NEWTON Home Page NEWTON Teachers Visit Our Archives Ask A Question How To Ask A Question Question of the Week Our Expert Scientists Volunteer at NEWTON! Frequently Asked Questions Referencing NEWTON About NEWTON About Ask A Scientist Education At Argonne Random files in BASIC
Name: James
Status: N/A
Age: N/A
Location: N/A
Country: N/A
Date: Around 1995


Question:
In BASIC, using random access files, is there a way to find out how many records are in a file (as in sequential, there is EOF)?



Replies:
BASIC, like most other programming languages, has no way of knowing how big a file is: you just read it until you reach the end. Having said that, I will now contradict myself! Your operating system (whatever that may be..) probably knows how big each file is: that information is kept in the file's header and/or the file system directory. So, if you know how that information is stored AND you can access the file headers or the directory files, then your BASIC (or any other language) routine can figure out file sizes. Many languages offer "hooks" into the operating system that provide, for example, system time or info on files. You have to check the particular implementation of your language for your operating system.

Hawley


Two possible approaches are:

1) Make all files the same length when you first create them

2) You may be able to use an "ON ERROR" statement to catch the end of file (It has been a while since I have done much with BASIC so I may be mistaken about this).

A third approach might be to write the number of records to the first record. Then you could expand the files but, of course, your program would have to keep track of any increases in file lengths.

I was just perusing my GWBasic manual. It has an LOF(n) function that returns the number of bytes allocated for the file. If you have a similar call you can just divide by the number of bytes per record to get the number of records.

G Bradburn


Click here to return to the Computer Science Archives

NEWTON is an electronic community for Science, Math, and Computer Science K-12 Educators, sponsored and operated by Argonne National Laboratory's Educational Programs, Andrew Skipor, Ph.D., Head of Educational Programs.

For assistance with NEWTON contact a System Operator (help@newton.dep.anl.gov), or at Argonne's Educational Programs

NEWTON AND ASK A SCIENTIST
Educational Programs
Building 360
9700 S. Cass Ave.
Argonne, Illinois
60439-4845, USA
Update: June 2012
Weclome To Newton

Argonne National Laboratory