Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C, Win32 API: l need help with ReadFile function[Solved]

C, Win32 API: l need help with ReadFile function[Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
helpjsonquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12105981
    wrote on last edited by
    #1

    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.

    J D 2 Replies Last reply
    0
    • U User 12105981

      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.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      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 is ERROR_NOACCESS / " Invalid access to memory location."

      U 1 Reply Last reply
      0
      • U User 12105981

        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.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Use double slashes in your file name. ReadFile() will read from the file pointer's current position. Perhaps you need to call SetFilePointer(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

        1 Reply Last reply
        0
        • J Jochen Arndt

          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 is ERROR_NOACCESS / " Invalid access to memory location."

          U Offline
          U Offline
          User 12105981
          wrote on last edited by
          #4

          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);
          
          L J 2 Replies Last reply
          0
          • U User 12105981

            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);
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You should check the return value from all system calls to see whether the function succeeded or failed. Without that information you are just guessing.

            1 Reply Last reply
            0
            • U User 12105981

              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);
              
              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              A probable error source is szBuf being NULL.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups