Read&Write

1231阅读 0评论2011-04-09 whj2819
分类:LINUX

// Just for test


#include
#include
#include
#include


int main(void)
{
    int fd = -1,ret = -1;
    char strings[] = "Hello World!";
    char buf[] = {0};


    fd = open("b.c",O_RDWR | O_CREAT | O_EXCL,0666);
    if ( !fd ) {
        printf("Open file error! \n");
    } else {
        //printf("fd = %d \n",fd);
        perror("b.c");
    }  

     ret = write(fd,strings,strlen(strings));
     if( -1 == ret ) {
        printf("ret = %d\n",ret );
        perror("strings");
    } else {
        perror("strings");
        printf("ret = %d\n",ret );
    }
    close(fd);

    fd = open("b.c",O_RDONLY);
    ret = read(fd,buf,strlen(strings) );
    printf("buf = %s\n",buf);

    close(fd);
    return 0;
}
上一篇:查看Linux 系统版本
下一篇:strcspn &strspn