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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using WriteFile [modified]

Using WriteFile [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 1 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.
  • C Offline
    C Offline
    capricious_001
    wrote on last edited by
    #1

    Hi there, I was wondering as to how one would use the WriteFile function. Say i have the following

    HANDLE hOutput;
    LPSTR = "Hello World";
    LPCVOID pBuffer;

    hOutput = CreateFile(TEXT("ouput.txt"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);

    WriteFile(hOutput, pBuffer, sizeof(pBuffer),&dwWritten, NULL);

    CloseHandle(hOutput);

    How would I "place" the "Hello World" string into the pBuffer? The output file is being created however there is some gibberish in the text and not what I want. As well, WriteFile does fail and does return false. Any help would be appreciated! Thanks, Robbie -- modified at 21:04 Sunday 18th June, 2006

    M 1 Reply Last reply
    0
    • C capricious_001

      Hi there, I was wondering as to how one would use the WriteFile function. Say i have the following

      HANDLE hOutput;
      LPSTR = "Hello World";
      LPCVOID pBuffer;

      hOutput = CreateFile(TEXT("ouput.txt"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL);

      WriteFile(hOutput, pBuffer, sizeof(pBuffer),&dwWritten, NULL);

      CloseHandle(hOutput);

      How would I "place" the "Hello World" string into the pBuffer? The output file is being created however there is some gibberish in the text and not what I want. As well, WriteFile does fail and does return false. Any help would be appreciated! Thanks, Robbie -- modified at 21:04 Sunday 18th June, 2006

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      The buffer is a pointer to the block of bytes you want to write: LPCSTR pData = "Hello World"; WriteFile(hOutput, (const void*) pData, strlen(pData), &dwWritten, NULL);

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

      C S 2 Replies Last reply
      0
      • M Michael Dunn

        The buffer is a pointer to the block of bytes you want to write: LPCSTR pData = "Hello World"; WriteFile(hOutput, (const void*) pData, strlen(pData), &dwWritten, NULL);

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

        C Offline
        C Offline
        capricious_001
        wrote on last edited by
        #3

        lol omg that is so embaressing. I was using bytes instead of strlen, so i was wondering why my stuff was getting cut off in the text file. However thanks alot for solving the problem for me Mike, I appreciate it. Robbie

        1 Reply Last reply
        0
        • M Michael Dunn

          The buffer is a pointer to the block of bytes you want to write: LPCSTR pData = "Hello World"; WriteFile(hOutput, (const void*) pData, strlen(pData), &dwWritten, NULL);

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          The (const void*) cast is not needed; all pointers can be implicitly cast to a void* pointer and a non-const pointer can be implicitly cast to a const one. Casts which aren’t needed often turn out to be maintenance problems or even bugs as a project evolves. Steve

          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