How to get a file's line count?

Author Topic: How to get a file's line count?  (Read 1697 times)

loop through file one time to find out line count
loop again and take care of business
i mean it works, you just loop twice to make things look pretty.

loop through file one time to find out line count
loop again and take care of business
i mean it works, you just loop twice to make things look pretty.
That's encredibly stupid.

That's encredibly stupid.

No it isn't? It's the perfect way to do it.

No it isn't? It's the perfect way to do it.
Yes it is stupid to read the file 2 times

Yes it is stupid to read the file 2 times
Store the information the first time...
... Then loop through the already read info...

Store the information the first time...
... Then loop through the already read info...
But that is not getting the ammount of lines BEFORE reading the file which is the question!

Also, just out of curiosity, why do you need a loading bar for loading a file? If your file is the big you are likely doing something wrong.

At the risk of being a heavy hypocrite, waltzing into Coding Help and saying 'solve this please' without saying why you need it is generally a bad idea because you might already be doing something wrong.

Brian is right; if your loading a file on the CLIENT-SIDE takes long enough to need a progress bar, you've already forgeted something up.

THEREFORE, I'm assuming that you're transferring files from a client to a server or vice versa. In which case you will need to read the entire file anyway, so just read the whole file into a storage object and then you have the line count inherently:

Code: [Select]
%file = new FileObject(%path);
%store = new SimObject();
while(!%file.isEOF())
    %store.line[-1+%store.lines++] = %file.readLine();
echo("Number of lines in "@fileName(%path)@" is "@%store.lines);

TorqueScript is a language that makes people fight because it's so insufficient.