Thread: ReadFile();

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    ReadFile();

    Could someone show me how to properley use the ReadFile(); function? I can't seem to figure out how to use it. Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    BOOL ReadFile(
      HANDLE hFile,                // handle of file to read
      LPVOID lpBuffer,             // pointer to buffer that receives data
      DWORD nNumberOfBytesToRead,  // number of bytes to read
      LPDWORD lpNumberOfBytesRead, // pointer to number of bytes read
      LPOVERLAPPED lpOverlapped    // pointer to structure for data
    );

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I think I understand what everything does now, but I still can't seem to make it work.
    Could you give me an example to work with? Thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    To read 10 bytes from a file:
    Code:
    char Buffer[10];
    DWORD dwBytes;
    HANDLE hFile;
    
    
    hFile=CreateFile... 
    ReadFile(hFile, Buffer, 10, &dwBytes, NULL);
    If a function dosen't work check the return code and call GetLastError to get an error code which will describe the problem in more detail.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    hFile=CreateFile...

    I don't get it. Why would you create a file just to read a file?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Because Create file is used to open files as well as create them.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I tried this, but if something is in my file it erases the contence then reads it. How can I prevent that?
    Code:
     char Buffer[10];
    DWORD dwBytes;
    HANDLE hFile; 
    hFile=CreateFile("draft.txt", GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ReadFile(hFile, Buffer, 10, &dwBytes, NULL);
    MessageBox(NULL, Buffer, "Found", MB_OK);
    Thanks.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    change CREATE_ALWAYS to OPEN_EXISTING

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Now it works. Thank for your help Quantum1024!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using ReadFile, WriteFile, strSafe
    By talz13 in forum C++ Programming
    Replies: 0
    Last Post: 02-08-2005, 12:37 PM
  2. ReadFile function fails.
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2004, 10:20 PM
  3. fread readfile
    By samsam1 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2003, 02:14 PM
  4. Using API function ReadFile()
    By Echidna in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2001, 10:33 PM
  5. ReadFile?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 10-23-2001, 01:13 AM