Notifications
Clear all

[Closed] filePos + eof problems

I’m trying to find out the length of a file using eof and filePos together, but it’s giving me completely incorrect results. Up until now I’d just been using readLine to read through every single line until reaching the end, but I figured using filePos would be much faster.

openedOBJ = openFile "C:\myFile.obj" mode:"r"
  seek openedOBJ #eof
  lengthOfFile = filePos openedOBJ
  flush openedOBJ ; close openedOBJ
 

Am I misunderstanding how the function is used?

Edit: Just figured it out. filePos get the current character, not the current file line.
Is there a good way to get the last line number in a file (without iterating through it via readLine) ?

5 Replies
1 Reply
(@denist)
Joined: 1 year ago

Posts: 0

what is the length of a file as you see it?

if you are looking for the number of lines:

((dotnetclass "System.IO.File").ReadAllLines file).count

but it will eat the memory

ss = openfile file
count = 0
while not eof ss do
(
    readline ss
    count += 1
) 
count

it can be more memory friendly

Yea I’d been doing that second method so far, just looking for quicker ways to get the info. Will try the first method. Thanks

the best method depends on of the file size… very big file with ReadAllLines will take a lot of memory and will not be read quickly

i guess that the solution with .net StreamReader would be the best