Hi,
what is diff bet fopen() & open, similarly fread() & read() so on...
which is better & which is preferable.
can any one explain with example
Printable View
Hi,
what is diff bet fopen() & open, similarly fread() & read() so on...
which is better & which is preferable.
can any one explain with example
First of all, I suggest that you read the manual and find out the difference between these functions yourself, by searching the web for "unix man function()".
Since you are starting out, I'd suggest that you learn how to use file streams. Functions which use file streams in your list include fopen and fread. Streams are a more robust interface for interacting with files, and that you should not use others like open and read, unless they provide a facility that you need to use.
Open and read are a part of a dated system for file I/O using file descriptors, and shouldn't be used for that purpose anymore really. Partly because read works by a chunk of bytes per every call; which can be slow for files at times, it's kinda like eating soup with a fork. What does make the file descriptor system useful however is that you can connect to other things besides files.
To use fopen, you need a path to a file first. Then you can create a FILE pointer for your program and do I/O operations.
For more details on reading from files, you can read a tutorial.Code:FILE *fp;
const char filename[] = "somefile.txt"; /*fopen's first parameter expects a character array
describing the file path */
if( !(fp=fopen(filename, "r")) ) /*open file for reading */
puts("error opening file"); /* always check fopen's return value to make sure
that you have a working file pointer. */
Thnx for ur reply, i got sufficient help.