readfile

This is a discussion on readfile within the Windows Programming forums, part of the Platform Specific Boards category; can someone explain how to use ReadFile easily? for the most basic of read operations? the code compiles but fails ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    26

    readfile

    can someone explain how to use ReadFile easily? for the most basic of read operations? the code compiles but fails on execution.

    Code:
    HANDLE file = CreateFile("filename",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,NULL);
    char buffer[1024];
    LPDWORD blah = 0;
    ReadFile(file,buffer,1024,blah,0);
    the file exists.
    Last edited by ZeroG; 09-21-2004 at 04:14 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    HANDLE file = CreateFile("filename",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
                             FILE_ATTRIBUTE_NORMAL,NULL);
    char buffer[1024];
    DWORD dwRead = 0;
    ReadFile(file, buffer, sizeof(buffer) - 1, &dwRead, 0);
    buffer[dwRead] = '\0';
    printf("Read %d bytes: %s\n", dwRead, buffer);
    Last edited by anonytmouse; 09-21-2004 at 05:36 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > LPDWORD blah = 0;
    > ReadFile(file,buffer,1024,blah,0);
    Everything expecting a pointer parameter invariably needs that parameter to point somewhere.
    Simply declaring a variable with the type which matches the parameter type is not enough. You typically need a variable with the dereferenced type (TYPE foo rather than LPTYPE foo), and pass that to the function using the address-of operator (&)

    [edit]
    Just like anonytmouse did
    [/edit]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    26
    ohh yeah. silly me. thanks.

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, 11:37 AM
  2. ReadFile function fails.
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2004, 09:20 PM
  3. fread readfile
    By samsam1 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2003, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21