在文件(文本或二进制)中移动的最快方法 [英] Fastest way to move in a file ( Text or Binary )

查看:81
本文介绍了在文件(文本或二进制)中移动的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


除了使用调用fseek或lseek之外,还有什么最快的方法可以移动文件中".

解决方案

我不知道.您为什么会这样呢,因为seek()函数在(可能)亚纳秒的时间内完成了? -打开句柄/描述符中的某个位置.我认为您不能使此速度更快.如果您打开一个文件到它的第1000000个字节的文件,则fseek不会从文件中读取第1000000个字节,它只是设置了前面提到的整数.寻求大于文件本身大小的偏移量甚至合法.如果将文件指针设置为大于文件大小的值,则fread()调用将返回而不会出现错误,表明只能读取零字节.在相同情况下,fwrite()会将文件大小增大到指定的偏移量,然后写出您的数据.由于安全原因,在某些系统上增长"文件以到达实际的文件指针可能会花费一些时间-(某些OS的)好习惯是覆盖分配来填补漏洞的扇区的先前内容位于文件末尾和文件指针之间,因为它们可能包含来自别人删除的某些文件(例如带有密码的文本文件)中的敏感数据.在Windows上,我知道具有管理特权,可以在预分配文件空间时使用API​​调用来关闭此覆盖过程.

无论如何,在您不想查找的文本文件中.我从未见过这样的程序.对于文本文件,您可以打开以使其文件指针为零读取(并且逐行读取文件),或者打开以文件指针为零并将文件大小设置为零(原始文件内容被删除)进行写操作,或者您可以通过添加原始内容并在打开后立即将filepointer设置为文件末尾来打开进行写操作.从理论上讲,您可以逐个字符地读取文本文件,但是我不知道如果混合使用函数,那么其中一个仅与字符(fgetc)一起使用,而另一个与行(fgets)一起使用会发生什么,永远不需要这个.


Hi,
Is there any fastest way to move "In a file" other than using the calls fseek or lseek.

解决方案

Not that I am aware of. Why do you think there could be, since the seek() functions complete in (probably) sub-nanosecond time?


Actually fseek and lseek do nothing more than setting an integer - the file pointer - somewhere in the open handle/descriptor. You can not make this faster I think. If you open a file an seek to its 1000000th byte fseek wont read up 1000000th bytes from the file, it just sets the integer mentioned earlier. Its even legal to seek to an offset that is bigger than the size of the file itself. If the file pointer is set to a value bigger than the file size then fread() calls will return without error indicating that only zero bytes could be read. fwrite() in the same situation will grow up the file size to the specified offset and then writes out your data. "Growing up" the file to reach the actual file pointer might take some time on some systems because of security reasons - it is a good habit (of some OSes) to overwrite the previous contents of the sectors that are allocated to fill in the hole between the end of the file and your file pointer because they might contain sensitive data from some files deleted by someone else (for example a text file with his passwords). On windows I know that with administrative privileges there are API calls to turn off this overwrite procedure when preallocating file space.

EDIT: anyway, in a text file you don''t want to fseek. I''ve never seen such a program. With text file you either open for read with the file pointer at zero (and you read the file line-by-line), or open for write with file pointer at zero and filesize set to zero (original file contents are deleted), or you open for write by appending to the original contents and the filepointer set to the end of file immediately after open. Theoritically you can read a text file character-by-character but I don''t know what happens if you mix functions so that one of them works with just characters (fgetc) and the other works with lines (fgets) but in practice I never in need of this.


这篇关于在文件(文本或二进制)中移动的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆