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. WriteFile help

WriteFile help

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testinghelp
8 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.
  • R Offline
    R Offline
    Ryan McDermott
    wrote on last edited by
    #1

    Ok I have no idea what I am doing wrong here: #include void main() { const char* buffer = "test"; DWORD byte; HANDLE hFile = CreateFile("C:\\testing.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hFile, buffer, NULL,&byte, 0); CloseHandle(hFile); } I am trying to create a file with text like "test". But it will not seem to write the text. It creates the file, but it won't write test. -Ryan M.

    M R 2 Replies Last reply
    0
    • R Ryan McDermott

      Ok I have no idea what I am doing wrong here: #include void main() { const char* buffer = "test"; DWORD byte; HANDLE hFile = CreateFile("C:\\testing.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hFile, buffer, NULL,&byte, 0); CloseHandle(hFile); } I am trying to create a file with text like "test". But it will not seem to write the text. It creates the file, but it won't write test. -Ryan M.

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

      Look at the parameters to WriteFile(), specifically the 3rd one. It's nNumberOfBytesToWrite. You're passing NULL, which is defined as 0, so you're saying write zero bytes, which is exactly what happens. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Actual sign at the laundromat I go to: "No tinting or dying."

      1 Reply Last reply
      0
      • R Ryan McDermott

        Ok I have no idea what I am doing wrong here: #include void main() { const char* buffer = "test"; DWORD byte; HANDLE hFile = CreateFile("C:\\testing.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hFile, buffer, NULL,&byte, 0); CloseHandle(hFile); } I am trying to create a file with text like "test". But it will not seem to write the text. It creates the file, but it won't write test. -Ryan M.

        R Offline
        R Offline
        Ryan McDermott
        wrote on last edited by
        #3

        ok i got it. thanks. I am an idiot for using NULL. I am still learning C++. I know vb much better ;) void main() { const char* buffer = "test"; DWORD byte; HANDLE hFile = CreateFile("C:\\testin.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hFile, buffer, 10,&byte, 0); CloseHandle(hFile); } -Ryan M.

        M M G 3 Replies Last reply
        0
        • R Ryan McDermott

          ok i got it. thanks. I am an idiot for using NULL. I am still learning C++. I know vb much better ;) void main() { const char* buffer = "test"; DWORD byte; HANDLE hFile = CreateFile("C:\\testin.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hFile, buffer, 10,&byte, 0); CloseHandle(hFile); } -Ryan M.

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

          You're still not passing the right number. Your source buffer is a string, which is 5 bytes long (4 letters plus the terminating 0). You're saying write 10 bytes, which makes WriteFile() read off the end of your string. This may crash, or if not it's writing random bytes to the file. Here's the preferred way:

          BOOL bSuccess = WriteFile ( hFile, buffer, strlen(buffer), &byte, NULL );

          Notice I'm checking the return value too ;) --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Actual sign at the laundromat I go to: "No tinting or dying."

          R 1 Reply Last reply
          0
          • M Michael Dunn

            You're still not passing the right number. Your source buffer is a string, which is 5 bytes long (4 letters plus the terminating 0). You're saying write 10 bytes, which makes WriteFile() read off the end of your string. This may crash, or if not it's writing random bytes to the file. Here's the preferred way:

            BOOL bSuccess = WriteFile ( hFile, buffer, strlen(buffer), &byte, NULL );

            Notice I'm checking the return value too ;) --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Actual sign at the laundromat I go to: "No tinting or dying."

            R Offline
            R Offline
            Ryan McDermott
            wrote on last edited by
            #5

            thank you so much! I will remember that next time :) -Ryan M.

            1 Reply Last reply
            0
            • R Ryan McDermott

              ok i got it. thanks. I am an idiot for using NULL. I am still learning C++. I know vb much better ;) void main() { const char* buffer = "test"; DWORD byte; HANDLE hFile = CreateFile("C:\\testin.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hFile, buffer, 10,&byte, 0); CloseHandle(hFile); } -Ryan M.

              M Offline
              M Offline
              Monty2
              wrote on last edited by
              #6

              Ryan McDermott wrote: I am still learning C++. I know vb much better Never too late to do the right thing :) C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg

              1 Reply Last reply
              0
              • R Ryan McDermott

                ok i got it. thanks. I am an idiot for using NULL. I am still learning C++. I know vb much better ;) void main() { const char* buffer = "test"; DWORD byte; HANDLE hFile = CreateFile("C:\\testin.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hFile, buffer, 10,&byte, 0); CloseHandle(hFile); } -Ryan M.

                G Offline
                G Offline
                Garth J Lancaster
                wrote on last edited by
                #7

                Ryan 1) I'd be a bit worried about the result if you used '10' as the size of the string as shown in your code ... you could crash the program, since you appear to be writing more characters than you have in the buffer .. 2) I'd use "test\0" to init the buffer and 3) strlen(buffer) as the 3rd argument for the WriteFile 'G'

                G 1 Reply Last reply
                0
                • G Garth J Lancaster

                  Ryan 1) I'd be a bit worried about the result if you used '10' as the size of the string as shown in your code ... you could crash the program, since you appear to be writing more characters than you have in the buffer .. 2) I'd use "test\0" to init the buffer and 3) strlen(buffer) as the 3rd argument for the WriteFile 'G'

                  G Offline
                  G Offline
                  Garth J Lancaster
                  wrote on last edited by
                  #8

                  I didnt realise Michael Dunn had already corrected your use of the '10' .. didnt see his post first time round sorry all 'G'

                  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