檢測檔案到末尾

ckkyjtqlt發表於2017-07-14
同步操作
// Attempt a synchronous read operation
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL);

// Check for eof
if (bResult &&  nBytesRead == 0, ) 
{
    // At the end of the file

}

非同步操作

// Attempt to initiate an asynchronous read operation.
bResult = ReadFile(hFile, 
                   &inBuffer, 
                   nBytesToRead, 
                   &nBytesRead, 
                   &gOverlapped);

// Check if there was a problem.
if (!bResult) 
{
    switch (dwError = GetLastError()) 
    {
        case ERROR_HANDLE_EOF: 
        // At the end of the file.
            break;
        case ERROR_IO_PENDING: 
        // I/O pending.
           break;
    }
}

// Check on an asynchronous read operation.
bResult = GetOverlappedResult(hFile, &gOverlapped, &nBytesRead, TRUE);

// Check if there was a problem.
if (!bResult) 
{
    switch (dwError = GetLastError()) 
    {
        case ERROR_HANDLE_EOF:
        // At the end of the file
    }
}

相關文章