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).
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.
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.
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() ).