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. HANDLE and FILE*

HANDLE and FILE*

Scheduled Pinned Locked Moved C / C++ / MFC
comjsonquestion
5 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.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George

    J CPalliniC V 3 Replies Last reply
    0
    • G George_George

      Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George

      J Offline
      J Offline
      Jonathan Darka
      wrote on last edited by
      #2

      You should be using WriteFile() with CreateFile().


      Jonathan Wilkes Darka[Xanya.net]

      1 Reply Last reply
      0
      • G George_George

        Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        As already suggested by Jonathan Wilkes: "you should be using WriteFile() with CreateFile()". Alternatively you can use fopen to open the file and then fwrite to write into the file itsef. Bottom line: be consistent. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • G George_George

          Hello everyone, I am using Windows API open (create) a new file by CreateFile, http://msdn2.microsoft.com/en-us/library/aa363858.aspx the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right? Are there any ways (or methods) to transform HANDLE to FILE*? I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct? thanks in advance, George

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          It seems easier to convert from FILE * to HANDLE:

          #include <io.h>
          #include <fcntl.h>
          
          FILE * f = . . .;
          HANDLE h = (HANDLE)_get_osfhandle(fileno(f));
          

          The other conversion looks longer:

          HANDLE h = . . .
          FILE * f = _fdopen(_open_osfhandle((intptr_t)h, _O_RDONLY), "rb");
          

          I hope this works. I am not sure mixing of operations with FILE * and HANDLE cannot be avoided.

          CPalliniC 1 Reply Last reply
          0
          • V Viorel

            It seems easier to convert from FILE * to HANDLE:

            #include <io.h>
            #include <fcntl.h>
            
            FILE * f = . . .;
            HANDLE h = (HANDLE)_get_osfhandle(fileno(f));
            

            The other conversion looks longer:

            HANDLE h = . . .
            FILE * f = _fdopen(_open_osfhandle((intptr_t)h, _O_RDONLY), "rb");
            

            I hope this works. I am not sure mixing of operations with FILE * and HANDLE cannot be avoided.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Viorel. wrote:

            I hope this works. I am not sure mixing of operations with FILE * and HANDLE cannot be avoided

            In fact the mix can usually be avoided (and should be avoided, at least to maintain the code coherence). There are, of course, exceptions... :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            In testa che avete, signor di Ceprano?

            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