Finding the number of lines in a text file
Finding the number of lines in a text file
(OP)
I'm trying to write a procedure to find the number of lines in a text file -- as far as I can see, the following *should* work, but it gives wildly innacurate results and also messes up the rest of the program (which works fine when the procedure is disabled). What am I doing wrong??
procedure chooseline;
var counter:integer;
begin
assign (listofwords, 'F:\project\words.txt');
reset (listofwords);
counter:=0;
while not eof (listofwords) do
begin
repeat
read (listofwords);
until eoln (listofwords);
counter:=counter+1;
readln;
end;
end;
procedure chooseline;
var counter:integer;
begin
assign (listofwords, 'F:\project\words.txt');
reset (listofwords);
counter:=0;
while not eof (listofwords) do
begin
repeat
read (listofwords);
until eoln (listofwords);
counter:=counter+1;
readln;
end;
end;
RE: Finding the number of lines in a text file
procedure chooseline;
var counter:integer;
ch:char;
begin
assign (listofwords, 'C:\pascal\hang.txt');
reset (listofwords);
counter:=0;
while not (eof (listofwords)) do
begin
repeat
read (listofwords,ch);
until eoln (listofwords);
counter:=counter+1;
readln;
end;
end;
RE: Finding the number of lines in a text file
RE: Finding the number of lines in a text file
close(listofwords);
RE: Finding the number of lines in a text file
(1) If you run your procedure on a file that doesn't exist... If you are writing turbo pascal, put {$I-} before the reset statement, right at the begining of a line (it's a compiler directive rather than a piece of pascal) to tell the compiler not to include error checking when the assign is carried out at run time. Error checking can be turned on again with {$I+} straight after the reset. Then check that IOerror is zero. If it isn't, then the assign didn't work. I think that IOerror resets to zero if it is checked, so if you need to use it several times in a row, set a variable to IOerror and test the variable.
e.g.
assign(myfile, 'sillyname.fl');
{$I-}
reset(myfile);
{$I+}
myErrVar := IOerror;
if myErrVar <> 0 then
begin
writeln('It all went pear-shaped! Error was: ', myErrVar);
halt(99);
end;
the rest of your code.
(2) If you work with a non-text file ('file') then the reset statement needs to be accompanied by a block size. If you don't quote one, it won't be 1 byte.
Therefore
var
myfile : file;
begin
assign(myfile, 'sillyname.bin');
reset(myfile);
blockread(myfile, bufferplace^, 45);
end;
does NOT read 45 bytes! Had it had reset(myfile, 1), it would have done.
I still keep making this mistake!
(3) As ppc386 pointed out, if you don't close your file, then sooner or later you will hit the maximum the OS is allowing you, and everythign will stop working. But that only exists within your programme: when it terminates, the OS will close any remaining files for you.
RE: Finding the number of lines in a text file
Cool ! I did not know this, no wonder I could never get BlockRead to work!
> when it terminates, the OS will close any remaining files for you.
This is true for files you are reading, but if you are writing to a file you MUST close it, or else it will not be written to disk.
RE: Finding the number of lines in a text file
Thanks for interesting stuff, everyone.
RE: Finding the number of lines in a text file
If you don't pass a block size parameter when resetting or rewriting untyped binary files, a standard block size of 128 bytes is assumed. The third parameter to blockread and blockwrite is the number of blocks to read. So in lionelhills example there will be read 128*45 = 5760 bytes.
Note that the entire block read must not be greater than 64K.
Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
http://student.vub.ac.be/~bvingerh/
Don't worry what people think about you. They're too busy wondering what you think about them.
RE: Finding the number of lines in a text file
RE: Finding the number of lines in a text file
If I have a problematic programme creating a log-file as it runs to help me diagnose the problem, then I am most likely to want to see the log file if the programme broke off execution with an error. And it is just here that the finish-off code is unlikely to have been run.
Good idea to set up an exit-proc to close the logfile!
RE: Finding the number of lines in a text file
RE: Finding the number of lines in a text file
RE: Finding the number of lines in a text file
Steven van Els
SAvanEls@cq-link.sr
RE: Finding the number of lines in a text file
I met it in turbo pascal 6.0, using the .bgi Borland graphic interface files, in an environment where only the network manager and I had write-access to the program's home directory. Unfortunately that prevented users from using the program, because for some reason it opens the .bgi files with read and write access. A very rare example of someone in Borland getting something wrong.
Sorry not to be able to offer any help, just sympathy.
RE: Finding the number of lines in a text file
You can set it before calling reset()
I think the values are something like this:
0 = open Read-Only
1 = open Write-Only
2 = open Read/Write
There is lots of good info on this stuff here...
http://www.efg2.com/Lab/Library/Delphi/IO/index.html
RE: Finding the number of lines in a text file
Lionel