Thread: It doesn't like my WriteFile();

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

    Exclamation It doesn't like my WriteFile();

    Why doesn't my compiler like this?
    Code:
    WriteFile("doucument1.txt","Data",0,NULL,OVERLAPPED);
    This is the error that comes up:
    expected primary-expression before ')' token
    Thanks in advance, August

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Probably because OVERLAPPED is the name of the structure. If you didn't open the file with FILE_FLAG_OVERLAPPED, change the last parameter to NULL.

    Edit: Source, msdn
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

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

    Question It doesn't like my WriteFile();

    Why doesn't my compiler like this?
    Code:
    WriteFile("doucument1.txt","Data",0,NULL,OVERLAPPED);
    This is the error that comes up:
    expected primary-expression before ')' token
    Why does it do this, I looked it up here:
    http://msdn.microsoft.com/library/en...asp?frame=true
    And I did it the way they said.

    Thanks.
    Last edited by Queatrix; 04-20-2005 at 05:48 PM.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Even after trying this:
    Code:
    WriteFile("doucument1.txt","Data",0,NULL,NULL);
    It comes up with this:
    invalid conversion from `const void*' to `void*'
    initializing argument 1 of `BOOL WriteFile(void*, const void*, DWORD, DWORD*, _OVERLAPPED*)'

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    My bad. Your first argument should be a handle returned by CreateFile (take a look at msdn for info on that function)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The basic steps in using winapi file io are:
    1. Obtain a file handle with CreateFile.
    2. Use that file handle to perform write operations (WriteFile) or read operations (ReadFile).
    3. Free the file handle when you're done with CloseHandle.


    There is an example on this msdn page which should hopefully set you straight on this.

    Edit: Thanks mods for merging invidious cross-post into single thread; much appreciated.
    Last edited by Ken Fitlike; 04-20-2005 at 07:51 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    When I use those the compiler doesn't catch an error or a warning, but when the program is executed a "Program Error" MessageBox comes up. Why is it doing that? I am using Dev-C++.

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    We're not mind readers...Where's your code?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    here is an example of using WriteFile
    Code:
    HANDLE hFile=CreateFile("file.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD dwBytes;
    if (hFile!=INVALID_HANDLE_VALUE)
    {
       WriteFile(hFile, "Data", 4, &dwBytes, NULL);
       CloseHandle(hFile);
    }
    else
       MessageBox(NULL, "Error opening file", NULL, MB_ICONERROR);
    Last edited by Quantum1024; 04-20-2005 at 10:57 PM.

  10. #10
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Hey! That works! Thanks, I think I understand it now, I was doing it wrong before. Just one question, would you know how to make breaks? "\n" doesn't work. Thanks.

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You might have to use "\r\n" if I recall correctly...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Wow. It worked. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WriteFile and fwrite
    By George2 in forum C Programming
    Replies: 4
    Last Post: 08-10-2007, 04:33 AM
  2. WriteFile issue
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-26-2007, 08:46 AM
  3. WriteFile question???
    By NewGuy100 in forum C Programming
    Replies: 27
    Last Post: 08-16-2005, 07:29 AM
  4. Whats wrong with my Writefile?
    By rEtard in forum Windows Programming
    Replies: 11
    Last Post: 06-10-2005, 02:46 PM