对于大文件,fread 会失败吗?

发布于 2024-09-25 15:05:08 字数 119 浏览 4 评论 0原文

我必须分析一个 16 GB 的文件。我正在使用 fread()fseek() 顺序读取文件。可行吗? fread() 能处理这么大的文件吗?

I have to analyze a 16 GB file. I am reading through the file sequentially using fread() and fseek(). Is it feasible? Will fread() work for such a large file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾铮苏瑾 2024-10-02 15:05:08

你没有提到语言,所以我假设是 C。

我没有发现 fread 有任何问题,但是 fseekftell可能有问题。

这些函数使用long int作为数据类型来保存文件位置,而不是像fpos_t甚至size_t这样的智能类型。这意味着它们可能无法处理超过 2 GB 的文件,并且肯定无法处理 16 GB 的文件。

您需要查看您的平台上的 long int 有多大。如果是 64 位的就可以了。如果是 32,则在使用 ftell 测量距文件开头的距离时可能会遇到问题。

考虑使用 fgetposfsetpos 代替。

You don't mention a language, so I'm going to assume C.

I don't see any problems with fread, but fseek and ftell may have issues.

Those functions use long int as the data type to hold the file position, rather than something intelligent like fpos_t or even size_t. This means that they can fail to work on a file over 2 GB, and can certainly fail on a 16 GB file.

You need to see how big long int is on your platform. If it's 64 bits, you're fine. If it's 32, you are likely to have problems when using ftell to measure distance from the start of the file.

Consider using fgetpos and fsetpos instead.

许你一世情深 2024-10-02 15:05:08

感谢您的回复。我知道我哪里出了问题。 fseek()ftell() 不适用于大于 4GB 的文件。我使用了 _fseeki64() 和 _ftelli64() ,现在工作正常。

Thanks for the response. I figured out where I was going wrong. fseek() and ftell() do not work for files larger than 4GB. I used _fseeki64() and _ftelli64() and it is working fine now.

幸福还没到 2024-10-02 15:05:08

如果正确实施,这应该不是问题。我假设按顺序你的意思是你正在以离散的块查看文件并前进你的文件指针。

查看 http://www. computing.net/answers/programming/using-fread-with-a-large-file-/10254.html

听起来他正在做和你几乎一样的事情。

If implemented correctly this shouldn't be a problem. I assume by sequentially you mean you're looking at the file in discrete chunks and advancing your file pointer.

Check out http://www.computing.net/answers/programming/using-fread-with-a-large-file-/10254.html

It sounds like he was doing nearly the same thing as you.

人间不值得 2024-10-02 15:05:08

这取决于你想做什么。如果您想要读取内存中的全部 16GB 数据,那么您很可能会耗尽内存或应用程序堆空间。

而是逐块读取数据并对这些块进行处理(并在完成后释放资源)。

但是,除此之外,还要决定您想要采用哪种方法(使用 fread()istream 等)并执行一些测试用例,看看哪种方法更适合您。

It depends on what you want to do. If you want to read the whole 16GB of data in memory, then chances are that you'll run out of memory or application heap space.

Rather read the data chunk by chunk and do processing on those chunks (and free resources when done).

But, besides all this, decide which approach you want to do (using fread() or istream, etc.) and do some test cases to see which works better for you.

再浓的妆也掩不了殇 2024-10-02 15:05:08

如果您使用的是 POSIX-ish 系统,则需要确保您构建的程序具有 64 位文件偏移支持。 POSIX 强制(或至少允许,并且大多数系统强制执行此操作)实现拒绝对大小不适合 off_t 的文件进行 IO 操作,即使正在执行的唯一 IO 是连续的且无需查找。

在 Linux 上,这意味着您需要在 gcc 命令行上使用 -D_FILE_OFFSET_BITS=64

If you're on a POSIX-ish system, you'll need to make sure you've built your program with 64-bit file offset support. POSIX mandates (or at least allows, and most systems enforce this) the implementation to deny IO operations on files whose size don't fit in off_t, even if the only IO being performed is sequential with no seeking.

On Linux, this means you need to use -D_FILE_OFFSET_BITS=64 on the gcc command line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文