访问大型文件在C [英] Accessing Large Files In C

查看:107
本文介绍了访问大型文件在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问使用C.大于2GB的文件在程序字节变量数量将从该文件并保存下一个位置的位置读取的一个运行。在文件中的位置读取程序和字节数在下次运行是阅读的位置开始。

I need to access a file larger than 2gb using C. During one run of the program a variable number of bytes will be read from the file and the location of the next position saved. During the next run of the program the file position is read and a number of bytes are read starting from that location.

并发症是偶尔的文件可能是COM pressed将其复制到一个新的文件,减去已经读取任何字节(我认为复制是做到这一点的唯一方法)。以这种方式去除的字节数也将被保存。

The complication is that occasionally the file may be 'compressed' by copying it to a new file, less any bytes that have already been read (I think copying is the only way to do that). The number of bytes removed in this way will also be saved.

我需要知道的文件从原来开始的当前位置与另一个文件同步。这应该是容易的,因为它只是(current_offset + deleted_bytes)。

I need to know the current position of the file from the original start for synchronizing with another file. This should be easy because it is just (current_offset + deleted_bytes).

的原因,它是不容易的是fseek的仅使用这限制了文件到2GB的长整型索引和fsetpos使用一个fpos_t结构的位置索引,这不是一个号码而无法转换来回一。我不知道任何方式用于文件定位很长的长整型指数,这将是理想的解决方案。

The reason it is not easy is the fseek only uses long int indexes which limits the file to 2gb, and fsetpos uses an fpos_t structure for the position index, which is not a number and can't be converted back and forth to one. I don't know of any way to use a long long int index for file positioning, which would be the ideal solution.

我该怎么办?

推荐答案

在Windows中可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/1yee101t%28v=vs.80%29.aspx\"><$c$c>_lseeki64()做64位的目的。

On windows you can use _lseeki64() to do 64 bit seeks.

有关与Linux兼容,还可以添加 -D_FILE_OFFSET_BITS = 64 在编译时间,然后做你的标题之一:

For compatibility with linux, you can also add -D_FILE_OFFSET_BITS=64 at compile time, and then do this in one of your headers:

#ifdef __MINGW32__ // or whatever you use to find out you're compiling on windows
#define lseek _lseeki64
#endif

然后用 lseek的()到处如常。这工作,因为Windows忽略 _FILE_OFFSET_BITS 标记和Linux将不会看到的重新定义 lseek的

then use lseek() everywhere as usual. This works because windows ignores the _FILE_OFFSET_BITS flag, and linux won't see the redefinition of lseek.

还有<一个href=\"http://msdn.microsoft.com/en-US/library/75yw9bf3%28v=VS.80%29.aspx\"><$c$c>_fseeki64()如果你preFER一个 FILE * 版本,并有相当的64位告诉() FTELL()功能太( _telli64() _ftelli64()

There's also _fseeki64() if you prefer a FILE* version, and there are equivalent 64 bit tell() and ftell() functions too (_telli64() and _ftelli64()).

这篇关于访问大型文件在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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