Friday, July 10, 2015

open VS fopen VS fdopen

open will return a filedes(int), while fopen and fdopen return a FILE*.

 #include <fcntl.h>
#include <stdio.h>

int main(void)
{
    char      pathname[] = "/tmp/myfile";
    int       filedes;
    FILE      *fp;

    if ((filedes = open(pathname, O_RDONLY)) == -1) {
        printf("open error for %s\n", pathname);
        return 1;
    }

    if ((fp=fdopen(filedes, "r")) == NULL) {
        printf("fdopen error for %s\n", pathname);
    } else {
        printf("fdopen seccess for %s\n", pathname);
        fclose(fp);
    }

    return 0;
}

No comments:

Post a Comment