函数 `feof` 总是 Returns 1
Function `feof` Always Returns 1
我正在尝试读入一个大小为 114,808 字节的二进制文件。我为文件分配内存,然后尝试使用 fopen
和 fread
读入文件。第一次调用 fread
时,它总是读入 274 字节,随后调用 feof
总是 returns 1,即使很明显还没有到达文件末尾。这是为什么?
根据 feof
的 MSDN documentation,"The feof function returns a nonzero value if a read operation has attempted to read past the end of the file; it returns 0 otherwise." 所以我不明白是什么导致 feof
始终 return 1.
这是我目前拥有的代码。任何帮助将非常感激。谢谢!
// Open source file with read-only access
if ( iError == ERROR_SUCCESS )
{
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
pFile = fopen( pSrcPath, "r" );
if ( pFile == NULL )
{
iError = ERROR_FILE_NOT_FOUND;
}
}
// Read in source file to memory
if ( iError == ERROR_SUCCESS )
{
do
{
// Buffer pointer passed to fread will be the file buffer, plus the
// number of bytes actually read thus far.
iSize = fread( pFileBuf + iActReadSz, 1, iReqReadSz, pFile );
iError = ferror( pFile ); // check for error
iEndOfFile = feof( pFile ); // check for end of file
if ( iError != 0 )
{
iError = ERROR_READ_FAULT;
}
else if ( iEndOfFile != 0 )
{
// Read operation attempted to read past the end of the file.
fprintf( stderr,
"Read operation attempted to read past the end of the file. %s: %s\n",
pSrcPath, strerror(errno) );
}
else
{
iError = ERROR_SUCCESS; // reset error flag
iActReadSz += iSize; // increment actual size read
iReqReadSz -= iSize; // decrement requested read size
}
}
while ((iEndOfFile == 0) && (iError == ERROR_SUCCESS));
}
// Close source file
if ( pFile != NULL )
{
fclose( pFile );
}
仅供参考,我正在尝试编写此代码,以便源代码或多或少与 C 兼容,即使 MSVS 基本上迫使您进入 C++ 环境。
您没有在 fopen 的模式字符串中包含 "b"。在 MS Windows 上,以文本模式打开文件将导致(在读取二进制文件时通常不希望发生的其他事情中)它在到达值为 0x1A 的字节时检测到 EOF。
我正在尝试读入一个大小为 114,808 字节的二进制文件。我为文件分配内存,然后尝试使用 fopen
和 fread
读入文件。第一次调用 fread
时,它总是读入 274 字节,随后调用 feof
总是 returns 1,即使很明显还没有到达文件末尾。这是为什么?
根据 feof
的 MSDN documentation,"The feof function returns a nonzero value if a read operation has attempted to read past the end of the file; it returns 0 otherwise." 所以我不明白是什么导致 feof
始终 return 1.
这是我目前拥有的代码。任何帮助将非常感激。谢谢!
// Open source file with read-only access
if ( iError == ERROR_SUCCESS )
{
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
pFile = fopen( pSrcPath, "r" );
if ( pFile == NULL )
{
iError = ERROR_FILE_NOT_FOUND;
}
}
// Read in source file to memory
if ( iError == ERROR_SUCCESS )
{
do
{
// Buffer pointer passed to fread will be the file buffer, plus the
// number of bytes actually read thus far.
iSize = fread( pFileBuf + iActReadSz, 1, iReqReadSz, pFile );
iError = ferror( pFile ); // check for error
iEndOfFile = feof( pFile ); // check for end of file
if ( iError != 0 )
{
iError = ERROR_READ_FAULT;
}
else if ( iEndOfFile != 0 )
{
// Read operation attempted to read past the end of the file.
fprintf( stderr,
"Read operation attempted to read past the end of the file. %s: %s\n",
pSrcPath, strerror(errno) );
}
else
{
iError = ERROR_SUCCESS; // reset error flag
iActReadSz += iSize; // increment actual size read
iReqReadSz -= iSize; // decrement requested read size
}
}
while ((iEndOfFile == 0) && (iError == ERROR_SUCCESS));
}
// Close source file
if ( pFile != NULL )
{
fclose( pFile );
}
仅供参考,我正在尝试编写此代码,以便源代码或多或少与 C 兼容,即使 MSVS 基本上迫使您进入 C++ 环境。
您没有在 fopen 的模式字符串中包含 "b"。在 MS Windows 上,以文本模式打开文件将导致(在读取二进制文件时通常不希望发生的其他事情中)它在到达值为 0x1A 的字节时检测到 EOF。