You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于_read()与read()函数的64位版本及大尺寸读取支持的技术问询

关于_read()与read()函数的64位版本及大尺寸读取支持的技术问询

Hey there, great question—this is such a common frustration when working with massive files on Windows, especially since we have _lseeki64 for 64-bit file positioning but no obvious 64-bit counterpart for _read()/read(). Let me break down what’s going on here and share some workarounds:

Why isn’t there a _read64()?

First, let’s clarify: the _read() function (Windows’ C runtime implementation) uses an unsigned int for the _MaxCharCount parameter, which caps single-read sizes at 4GB (since that’s the max value for a 32-bit unsigned int—you mentioned 2GB, maybe you were thinking of signed int limits?).

Microsoft hasn’t rolled out a dedicated _read64() function for a few reasons:

  • The C standard library’s read() (which _read() mirrors) is rooted in POSIX standards, which historically used size_t for the count parameter. On 64-bit Windows, size_t is 64-bit, but the CRT’s _read() stuck with unsigned int for backward compatibility.
  • Realistically, even if there was a 64-bit parameter, your OS wouldn’t let you read that much data in a single call anyway—physical/virtual memory limits make large single reads impractical. The design assumes you’ll loop through smaller chunks.
  • The _lseeki64 exists to handle 64-bit file offsets (since files can easily exceed 4GB), but reading operations don’t need a 64-bit count parameter to work with those large files—you just read in chunks from the 64-bit offset.

Practical Workarounds for Large File Reads

If you need to handle reads that would exceed the 32-bit count limit, here are your best options:

  • Use the Win32 API ReadFile directly: This is the low-level API that _read() wraps under the hood. While its byte-count parameter is still a 32-bit DWORD, you can loop calls to ReadFile, each time reading the maximum 32-bit chunk, until you’ve pulled all the data you need. Pair this with SetFilePointerEx (the 64-bit equivalent of _lseeki64) for precise 64-bit offset positioning.
  • Compile for 64-bit: Even though _read()’s count parameter stays 32-bit, a 64-bit process has access to far more virtual memory, making it feasible to allocate larger buffers and loop through multiple _read() calls to assemble large datasets.
  • Use C++ standard library tools: If you’re working in C++, std::ifstream (when compiled for 64-bit) supports 64-bit file positions via std::streampos. Its read() method uses std::streamsize (typically 64-bit on 64-bit systems) for the count, and you can loop through reads to handle large files smoothly.

Quick side note: No matter which method you use, you’ll almost always want to read large files in manageable chunks (like 64KB or 1MB) rather than trying to load the entire file into memory at once—it’s more efficient and avoids memory overflow issues.

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 11:39:33