Thread: Diff bet fopen() & open()

  1. #1
    Registered User
    Join Date
    May 2006

    Diff bet fopen() & open()

    what is diff bet fopen() & open, similarly fread() & read() so on...

    which is better & which is preferable.

    can any one explain with example

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    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.
        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. */
    For more details on reading from files, you can read a tutorial.

  3. #3
    Registered User
    Join Date
    May 2006
    Thnx for ur reply, i got sufficient help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 05:27 AM
  2. Question about fopen
    By IxTanGxI in forum C Programming
    Replies: 5
    Last Post: 11-15-2005, 03:34 PM
  3. open or fopen?
    By Tibo in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 09:14 AM
  4. Command to open the 'Open Dialog Box' found in most programs.
    By OmegaFirebolt in forum Windows Programming
    Replies: 5
    Last Post: 03-16-2003, 07:58 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM
Website Security Test