Is there an easy way to programmatically determine the number of lines within a text file?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I've tried different ways and the fastest if you have very large file is:
This would use less memory, but probably take longer
A viable option, and one that I have personally used, would be to add your own header to the first line of the file. I did this for a custom model format for my game. Basically, I have a tool that optimizes my .obj files, getting rid of the crap I don't need, converts them to a better layout, and then writes the total number of lines, faces, normals, vertices, and texture UVs on the very first line. That data is then used by various array buffers when the model is loaded.
This is also useful because you only need to loop through the file once to load it in, instead of once to count the lines, and again to read the data into your created buffers.
If by easy you mean a lines of code that are easy to decipher but per chance inefficient?
That's probably the quickest way to know how many lines.
You could also do (depending on if you are buffering it in)
There are other numerous ways but one of the above is probably what you'll go with.
count the carriage returns/line feeds. I believe in unicode they are still 0x000D and 0x000A respectively. that way you can be as efficient or as inefficient as you want, and decide if you have to deal with both characters or not