fopen and open

what is the difference between

fopen and open
fread and read
fwrite and write
open and create

why this much of functions for the i/o when everything does the same...?
What is their major difference?
In which case, which is the best to use.

:confused:'ed Collins

It's "creat", not "create". Originally, the open system call could only open files that existed. Now that you can do "open(file, O_CREAT|O_TRUNC|O_WRONLY, mode)", creat no longer has any great use. But lots of code had been written using creat, so it persists. Don't use creat, it's day is long past.

The system calls like read, write, and open must exist if programs are going to be able to use the kernel's drivers. Stuff like fopen, fclose, fread, etc are put of the standard I/O library or stdio. stdio includes other routines like getchar, putchar, etc. So not all of the names start with f. stdio was developed as part of the C language. When C is implemented on other platforms, the stdio library will be there (the ansi C standard mandates this). This is not necessarily true of the unix system calls. So to write C code truely portable to non-unix systems, you must use stdio.

If your program is intended for unix systems only, the most important factor is i/o buffering. If you want to read a file character by character, issuing separate read system calls is slow. A system call is expensive. Even with a disk file, where the data is being fetched from the buffer cache, a separate read for each character will take too long. By using stdio, one read will happen to load some data into the library's buffer. Now if you read character by character, it will go much faster. And that's with a disk file which is using a buffer cache. If you are doing i/o to, say, /dev/tty, there is no buffer cache. Each read and write goes all the way to the driver. On the other hand, if you are reading a disk file block by block, the stdio buffering will probably slow you down. The data is read from the disk into the buffer cache. Then it's copied from the buffer cache into stdio's buffer. Then it's copied from stdio's buffer into your program. So for each application, you need to consider which technique is faster.

Another consideration is that stdio has a richer feature set than is available directly from the system call interface. You can do an ungetc() but there is no unread(). And printf has a ton of features that write() can't do.

kindly elaborate about how character by charatcter read operation through library call will be faster than read system call ,done charatcer by character