C, Win32 API: l need help with ReadFile function[Solved]
-
Happy New Month to you all. However, l am playing with Createfile, WriteFile and ReadFile functions, Createfile and Writefile works well but my problem is ReadFile. l want to read the data written to a buffer "szBuffer", by using "ReadFile" to read the data in the szBuffer into another buffer szBuf. But it doesn't work. When l check the return value like error=ReadFile(hfile,szBuffer,10,&in,NULL), it returns 0 but when l use GetLastError() to check for the return value, it gives me 998. l learnt that the second parameter to ReadFile() is the address to store the data read. Code snippet below:
hfile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data CloseHandle(hfile); hdc=GetDC(hwnd); TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value ReleaseDC(hwnd,hdc);
Can someone help me to overcome this? Thanks.
-
Happy New Month to you all. However, l am playing with Createfile, WriteFile and ReadFile functions, Createfile and Writefile works well but my problem is ReadFile. l want to read the data written to a buffer "szBuffer", by using "ReadFile" to read the data in the szBuffer into another buffer szBuf. But it doesn't work. When l check the return value like error=ReadFile(hfile,szBuffer,10,&in,NULL), it returns 0 but when l use GetLastError() to check for the return value, it gives me 998. l learnt that the second parameter to ReadFile() is the address to store the data read. Code snippet below:
hfile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data CloseHandle(hfile); hdc=GetDC(hwnd); TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value ReleaseDC(hwnd,hdc);
Can someone help me to overcome this? Thanks.
After writing to the file, the file pointer is at the actual end of the file when the file has been created. If you then try to read from the file, there is nothing to read at that position (the end of the file). If you want to read the data that has just been written, you must set the file pointer using SetFilePointer function (Windows)[^]:
// Rewind to begin of file
SetFilePointer(hFile, 0, 0, FILE_BEGIN);A return value of zero (
FALSE
) indicates that the function failed (see ReadFile function (Windows)[^]). Error code 998 isERROR_NOACCESS
/ " Invalid access to memory location." -
Happy New Month to you all. However, l am playing with Createfile, WriteFile and ReadFile functions, Createfile and Writefile works well but my problem is ReadFile. l want to read the data written to a buffer "szBuffer", by using "ReadFile" to read the data in the szBuffer into another buffer szBuf. But it doesn't work. When l check the return value like error=ReadFile(hfile,szBuffer,10,&in,NULL), it returns 0 but when l use GetLastError() to check for the return value, it gives me 998. l learnt that the second parameter to ReadFile() is the address to store the data read. Code snippet below:
hfile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data CloseHandle(hfile); hdc=GetDC(hwnd); TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value ReleaseDC(hwnd,hdc);
Can someone help me to overcome this? Thanks.
Use double slashes in your file name.
ReadFile()
will read from the file pointer's current position. Perhaps you need to callSetFilePointer(FILE_BEGIN)
in between write and read."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
After writing to the file, the file pointer is at the actual end of the file when the file has been created. If you then try to read from the file, there is nothing to read at that position (the end of the file). If you want to read the data that has just been written, you must set the file pointer using SetFilePointer function (Windows)[^]:
// Rewind to begin of file
SetFilePointer(hFile, 0, 0, FILE_BEGIN);A return value of zero (
FALSE
) indicates that the function failed (see ReadFile function (Windows)[^]). Error code 998 isERROR_NOACCESS
/ " Invalid access to memory location."Thanks. But l have made necessary corrections but the ReadFile is still not working and GetLasterror still returns 998. Also, direct check of ReadFile also returns 0. Help please.
Quote:
hfile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well SetFilePointer(hfile,0,0,FILE\_BEGIN); ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data CloseHandle(hfile); hdc=GetDC(hwnd); TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value ReleaseDC(hwnd,hdc);
-
Thanks. But l have made necessary corrections but the ReadFile is still not working and GetLasterror still returns 998. Also, direct check of ReadFile also returns 0. Help please.
Quote:
hfile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well SetFilePointer(hfile,0,0,FILE\_BEGIN); ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data CloseHandle(hfile); hdc=GetDC(hwnd); TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value ReleaseDC(hwnd,hdc);
-
Thanks. But l have made necessary corrections but the ReadFile is still not working and GetLasterror still returns 998. Also, direct check of ReadFile also returns 0. Help please.
Quote:
hfile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well SetFilePointer(hfile,0,0,FILE\_BEGIN); ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data CloseHandle(hfile); hdc=GetDC(hwnd); TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value ReleaseDC(hwnd,hdc);
A probable error source is
szBuf
beingNULL
.