首先,我如何取得一个文本文件的行数?( 不要告诉我用fgets先循环一遍:) )
第二,我怎么把文件指针定位在某一行?比如我想从第28行开始读取,怎么办? ( 也不要告诉我使用fgets循环28次 :) )
多谢!!
不可能不这样计算的。
in Unix system the return charactor is \0x0A, same to DOS.
in Mac system it is \0x0D, in Windows there are to charactors to represent return.
you must find out all return charactor, and counter them.
but some thing can help you, like std::string and stl::fstream
Agree with fireseed. You must let the system to parse the file.
You must understand the basic principle of a text file: it is a sequential access storage. Unlike database, which is ramdon access storage, you must iterate to get to a point.
没有捷径,必须一行一行的找。
只有扫描,判断行的结束符0x0a,用一计数器进行统计。
靠!以为一脚踏进了银河系中心,这么多星星~
我就说我的三角和星怎么都没了。
应该不会有捷径的。
你想,“找到第28行”这件事,要么由库函数来做,要么你自己做,但肯定不可能是CPU直接做,CPU要有那么聪明,机器人早就统治整个世界了。^_^
既然如此,这样的库函数有没有还有什么区别吗?唯一的区别就是库函数是别人写的,而如果你自己编函数,就是你自己写的。你自己省点力气而已,CPU一点也感觉不到。
steedhorse(晨星)说的对:
即使你用 TStringList 事实上系统也是这么一行行数下来的。
差别只是你自己数还是别人帮你数罢了。
I agree with the above. You must find it one by one .
除非每行建个索引,记录了每一行的起始位置在文件中的绝对偏移值,就可以实现直接跳转到xx行之类的功能。