_read() and read() 3rd argument (_MaxCharCount) is unsigned int, which limits the read to 2GB, is there a version that uses a 64 bit third argument? because there is _lseeki64 so doesnt make sense why no 64 bit read function.
You’re absolutely right to notice the inconsistency: _read()
and read()
in the C runtime API have a 32-bit unsigned int
parameter for the number of bytes to read, which effectively limits a single read operation to 2^32−1 bytes (4 GB) — though in practice, much less due to system limits. This limitation feels inconsistent given that _lseeki64()
allows seeking beyond 4 GB, enabling 64-bit file sizes.
Short Answer:
There is no 64-bit version of _read()
or read()
that takes a 64-bit byte count — i.e., no _read64()
or similar exists in the standard MSVC runtime or POSIX.
Why This Limitation Exists:
- Legacy API Design:
read()
and_read()
are based on classic UNIX and C runtime APIs designed decades ago, when 32-bit systems were standard. - OS and Hardware Limits: Operating systems (Windows, Linux) internally place limits on the max size of a single I/O operation — typically far less than 4 GB anyway. Trying to read several gigabytes in one call is not practical or efficient.
- Real-world usage: You can always loop
read()
in chunks to process larger data — and that’s the standard approach.
Recommended Approach for Large File I/O:
- Use
_lseeki64()
orSetFilePointerEx()
for positioning within files over 4 GB. - Use a loop with
_read()
orReadFile()
to read in chunks (e.g., 64 KB, 1 MB, etc.) - If you’re working at a higher level, consider using memory-mapped files (
CreateFileMapping
/MapViewOfFile
) or stream libraries that abstract this.
Example Loop (Reading > 2 GB):
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
const size_t CHUNK_SIZE = 1024 * 1024; // 1 MB
void readLargeFile(const char* filePath) {
int fd = _open(filePath, _O_RDONLY | _O_BINARY);
if (fd == -1) {
perror("open");
return;
}
char* buffer = new char[CHUNK_SIZE];
__int64 totalRead = 0;
int bytesRead;
while ((bytesRead = _read(fd, buffer, (unsigned int)CHUNK_SIZE)) > 0) {
totalRead += bytesRead;
// process buffer
}
printf("Total bytes read: %lld\n", totalRead);
delete[] buffer;
_close(fd);
}
If You’re Using Win32 APIs Instead:
Use ReadFile()
with OVERLAPPED
for large file offsets and async I/O.
Would you like an example using ReadFile()
with OVERLAPPED
to handle large files?