What are differences between fopen() and open() ?

This is a discussion on What are differences between fopen() and open() ? within the C++ Programming forums, part of the General Programming Boards category; Both can open a file: Code: FILE * fp = fopen(fname, "r"); int fd = open(fname, O_RDONLY, 0777); What are ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What are differences between fopen() and open() ?

    Both can open a file:
    Code:
    FILE * fp = fopen(fname, "r");
    
    int fd = open(fname, O_RDONLY, 0777);
    What are the differences? Why I see people use fopen more often? Do they belong to different libraries?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    fopen() is standard, portable C.

    open() is a POSIX function which is not standard C and not portable in general.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by brewbuck View Post
    fopen() is standard, portable C.

    open() is a POSIX function which is not standard C and not portable in general.
    Thanks, but what is not portable? Is that to say, I can't use POSIX in some platforms, say Win XP?

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Thanks, but what is not portable? Is that to say, I can't use POSIX in some platforms, say Win XP?
    Exactly. In a practical sense (on the desktop at least), POSIX is everything except Windows.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    On a side note, open() exists very much on Microsoft's implementation. In fact, fopen calls open.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Isn't it called _open, though?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Something along that line, I think. But it does take the open function arguments...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    Isn't it called _open, though?
    The documentation lists a lot of functions that begin with _ but the compiler still accepts it without the _ (although you might need to define that flag not to deprecate functions).

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Regardless of whether Windows has it, it's a non-standard function which can't be used if your goal is to write purely portable programs.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    Regardless of whether Windows has it, it's a non-standard function which can't be used if your goal is to write purely portable programs.
    Yeah, but I don't think there is such a thing as a purely portable program, since there's always going to be some hardware that doesn't support C or C++. But most of the time you'd define exactly what kind of platforms you'll support up front. If you're writing a game for instance; who cares if it's not portable to an intelligent toaster oven.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen can't open .exe?
    By fanoliv in forum C Programming
    Replies: 11
    Last Post: 04-05-2011, 03:24 PM
  2. fopen and open
    By ygfperson in forum Linux Programming
    Replies: 2
    Last Post: 06-29-2005, 08:34 PM
  3. open, fopen ?
    By Matrim in forum C Programming
    Replies: 2
    Last Post: 02-01-2005, 08:45 AM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

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