There are differences between open()
and fopen()
function. One is system call and other is library function. I try to figure out what is the application of these two function but I found nothing useful. Can you give some scenarios where to use open()
and where fopen()
should be used?
Asked
Active
Viewed 281 times
0

Amit
- 81
- 5
-
1The `open` function is only a system call on some POSIX systems. On e.g. Windows it's a normal library function just like `fopen`. – Some programmer dude Mar 05 '14 at 12:37
2 Answers
3
Sometimes you need a file descriptor. open()
gives you one. Sometimes you need a FILE*
, in which case use fopen()
. You can always turn your FILE*
into a file descriptor via fileno()
, whereas the opposite transformation is not really supported. It mostly depends on what downstream functions you intend to call with the file handle.

honk
- 9,137
- 11
- 75
- 83

John Zwinck
- 239,568
- 38
- 324
- 436
-
-
1When you only want to call functions that require a file descriptor, and no functions that require a FILE*. – John Zwinck Mar 05 '14 at 12:43
0
open()
will return the file descriptor. We overwrite the file, while using the fopen()
we cannot overwrite the file. We use the file descriptor for reading and writing using the other
functions like read()
, write()
, etc. But in fopen()
it will return file descriptor we have to use fprintf()
to write to the file stream. sscanf()
to read from the stream.

user16217248
- 3,119
- 19
- 19
- 37

Q_SaD
- 355
- 1
- 11
-
1We can use other functions like lseek to write at the middle of the file too. – Q_SaD Mar 05 '14 at 12:44