read() vs fread()

hi,
i have a question about c filestreams. there are so many differnt types of reading from/writing to files in c/c++ and i don't realy know what's best to use.
espacialy i noticed that there are fuctions like fread wich exspect a FILE * structure as filedescriptor and fuctions like read wich exspect an int.
i'd like to know what's the difference and how to deal with an filedescriptor of the type FILE* if i want to use it in a function that expects an int as fd (i.e. mmap).

sincerly,
ritka
To open a file you can use fopen, which returns a FILE* that you can with the fread function. This is the standard C way of handling file IO.

read is a POSIX function (not standard C). The int that you pass to it is called a file descriptor. You can use the open function to get a file descriptor for a file.
ah, ok. and is there a way to get the filedescriptor from the FILE structure?
not necessarily, FILE Structures oftenly goes hand in hand with databases and schemas well that's if we go with an overview of programming
fread is buffered. read is unbuffered.
ok. so what would you prefer? and why? and in wich situation, of course?
Last edited on
fread is Standard C. Use that.
my last question: why are there these posix functions although there are standard c functions with the same purpose?
Standard C is portable. POSIX is an Operating System standard, and isn't expected to be portable.

So the implementers of fread() will call read() on POSIX, and ReadFile() in Windows for example. And the programmer is expected to call fread(), which will be available everywhere.
Last edited on
yes, i know. but i don't understand why read() exists since you can use fread()
Why would you want a buffered read versus a non-buffered read? If you need to read a byte of data when it's ready, use a non-buffered read (read() ). If you are reading data but are happy with the buffering the operating system supplies, use a buffered read (fread() ).
i don't understand why read()
It the read operation provided by the Operating System.
think i got it now. thanks for helping
question?well.
i will give you a appropriate answer.
Topic archived. No new replies allowed.