fopen and CreateFile

This is a discussion on fopen and CreateFile within the Windows Programming forums, part of the Platform Specific Boards category; Could someone please explain the difference between fopen and CreateFile to me. I hadn't seen createfile until recently when browsing ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    fopen and CreateFile

    Could someone please explain the difference between fopen and CreateFile to me.

    I hadn't seen createfile until recently when browsing these forums. What are there pros and cons of each that I should be aware of?

    Here is what I found a MS's site:
    Code:
    HANDLE hFile; 
     
    hFile = CreateFile("MYFILE.TXT",           // create MYFILE.TXT 
                 GENERIC_WRITE,                // open for writing 
                 0,                            // do not share 
                 NULL,                         // no security 
                 CREATE_ALWAYS,                // overwrite existing 
                 FILE_ATTRIBUTE_NORMAL |       // normal file 
                 FILE_FLAG_OVERLAPPED,         // asynchronous I/O 
                 NULL);                        // no attr. template
    It seems to me that this accomplishes basically the same thing as fopen would but obviously allows for other options like: sharing and security as per the remarks.
    Am I on the right track?
    Also, when programming for windows should I generally use createfile or just save this for when I need to use these additional features?
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> options like: sharing and security as per the remarks.

    Indeed. You can also open a good deal more than a file with CreateFile(), it is a very powerful API routine, unlike fopen() which is a C hangover.

    Want a handle to the parallel port? CreateFile().
    Need a pipe? CreateFile().
    Mailslot perhaps? CreateFile().
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    fopen() is the part of the standard C-library, which is implemented using an OS dependent file I/O interface - this is CreateFile() on Window's machines.
    Similiarly, open() is the OS interface for file I/O on most *nix OS's.

    gg

  4. #4
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Thanks for the info. I had no idea CreateFile was so powerful.

    I certainly appreciate your responses.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the equivalent of fopen(..) in API's CreateFile
    By Joelito in forum Windows Programming
    Replies: 12
    Last Post: 11-28-2007, 07:14 PM

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