fopen() and open()

This is a discussion on fopen() and open() within the C Programming forums, part of the General Programming Boards category; hi all, i have a question, whats the big difference between fopen() and open()? what are the pro's and con's ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    24

    fopen() and open()

    hi all,

    i have a question,
    whats the big difference between fopen() and open()?
    what are the pro's and con's of both functions,

    because you can open/wite/read files with both of them..
    I find open() slightly harder to use then fopen().
    But is it that open is UNIX/Linux only and fopen works on both?

    thanks in advance

    enrypted

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Function fopen() opens a file and returns a pointer to FILE. You need to include stdio.h for it.

    FILE *fopen (const char * restrict filename, const char * restrict mode);

    Function open() is not a standard C function. It is a system call to open a file or device. To use it, you must include fcntl.h. But since it is not a standard C function, it may not be available in your system. Unix-like systems usually have it.

    int open(const char *path, int flags, ...);

    The pro of using fopen() is that it is standard.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Ok, thanks

    so the bottom line is; fopen() is standard
    open() is a UNIX system call


    encrypted

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    open has a lot of extra features. Check that man page (only if you're able to use it of course)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Ok thanks,

    but i have a problem now:
    Code:
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int fd1, fd2;
            char *buffer;
    
            fd1 = open( "dude.dat", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR );
    
            fd2 = open( "dude.dat", O_RDONLY );
    
            write( fd1, "UNIX", 5 );
    
            read( fd2, buffer, 5 );
    
            printf("%s\n", buffer );
    
            close( fd1 );
            close( fd2 );
    
            exit(0);
    }
    If i compile this code ( with gcc ) and run it, i dont get 'UNIX' on my screen but some weird characters that i've never seen before


    Anyone know what im doing wrong?

    thank you

    encrypted

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    First, always perform error-checks. Check if the value that function open() returns is no error-value. In case open() returns an error-value, you cannot use that file-descriptor.

    Second, you try open the same file twice. Try an algorithm like this:

    - open the file for writing
    - write to it
    - close the file
    - open the file for reading
    - read from it
    - close the file

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Thanks man

    i got it working,
    and btw is this what you suggested by error checking? :

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    int main()
    {
            int fd;
            char buffer[5];
    
            if( ( fd = open( "data.dat", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR ) ) != -1 )
            {
                    write( fd, "UNIX", 5 );
            }
            else
            {
                    printf("File could not be opened");
                    exit(1);
            }
    
            close( fd );
    
            if( ( fd = open( "data.dat", O_RDONLY ) ) != -1 )
            {
                    read( fd, buffer, 5 );
            }
            else
            {
                    printf("File could not be read");
                    exit(1);
            }
    
            close( fd );
    
            printf("Result read from file:\n %s\n", buffer );
    
             exit(0);
    }
    encrypted

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Yes that is what I meant with error-checking. A lot of standard functions return a specific value when an error occurred, checking the return values and take the right action when an error occurred results in more robust programs. When you don't perform error-checking, it is the same as assuming everything will always go right, which may result in undesirable behaviour of the code. Using error-checking you correct such behaviour.

    [edit]
    BTW, using function exit() is not necessary here. You could just use return 0 and return 1.
    [/edit]

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>read( fd, buffer, 5 );
    Unlike fgets(), read() won't terminate the array with a nul byte, so doing this might produce strange results:
    >>printf("Result read from file:\n %s\n", buffer );

    If you want to print the array as a string, make sure its nul terminated first.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen can't open .exe?
    By fanoliv in forum C Programming
    Replies: 11
    Last Post: 04-05-2011, 03:24 PM
  2. What are differences between fopen() and open() ?
    By meili100 in forum C++ Programming
    Replies: 9
    Last Post: 04-08-2008, 10:14 AM
  3. fopen and open
    By ygfperson in forum Linux Programming
    Replies: 2
    Last Post: 06-29-2005, 08:34 PM
  4. open, fopen ?
    By Matrim in forum C Programming
    Replies: 2
    Last Post: 02-01-2005, 08:45 AM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21