I am calling ReadFile() WinAPI to copy the file contents to a char array, inside my VC++ code. Have placed GetLastError() immediately after ReadFile().
for( read some n no: of files)
{
FileRead(fp,destCharArray,ByesToRead,NoOfBytesRead,NULL);
int ret = GetLastError();
}
GetLastError() is returning 183 only when 1st file is read. For all other file reads its returning 183. But eventhough 183 is returned the contents of file are copied to charArray. And the problem is that the file read does not happen for some 28th file (here too return status is 183). Irrespective of successful or unsuccessful file read, 183 is returned!
According to http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
error code 183 means "ERROR_ALREADY_EXISTS".
What does the above error status signify in ReadFile() context.?
Can anyone kindly help me in figuring out why?
Your code is incorrectly calling
GetLastError
. You should only callGetLastError
if the immediately prior Win32 API call failed, and that API returns status information throughGetLastError
.Here the API in question is
ReadFile
. The documentation says:In other words you must only call it if
ReadFile
returnsFALSE
.Your code should look something like this:
Your code is returning the error code for an earlier failure that is unrelated to the call to
ReadFile
.