码迷,mamicode.com
首页 > 其他好文 > 详细

文件I/O的操作实例

时间:2016-05-23 19:04:48      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

1_open.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
    int fd;        //文件描述符
    
    /*以读的方式打开一个文件,如果文件不存在,则返回错误*/
    fd = open("test.txt",O_RDONLY);
    if(fd < 0){
        perror("open");
        return -1;
    }
    printf("fd == %d\n",fd);

    return 0;
}

2_open.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
    int fd;        //文件描述符
    
    /*以读的方式打开一个文件,如果文件不存在,则创建它*/
    fd = open("test.txt",O_RDONLY | O_CREAT,0755);
    if(fd < 0){
        perror("open");
        return -1;
    }
    printf("fd == %d\n",fd);

    return 0;
}

3_read.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
    int fd;        //文件描述符
    int ret;
    char buf[100] = {0};

    /*读取标准终端 : 0 (STDIN_FILENO)*/
    ret = read(0,buf,sizeof(buf)-1);
    if(ret > 0){
        /*读取成功*/

        /*打印到终端*/
        printf("%s\n",buf);
    }

    return 0;
}

4_read.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
    int fd;        //文件描述符
    int ret;
    char buf[100] = {0};

    /*打开一个文件,以读的方式*/
    fd = open("test.txt",O_RDONLY);
    if(fd < 0){
        perror("open test");
        return -1;
    }
    
    /*把文件指针移动到文件头*/
    lseek(fd,0,SEEK_SET);

    /*读取标准终端 : 0 (STDIN_FILENO)*/
    ret = read(fd,buf,sizeof(buf)-1);
    if(ret > 0){
        /*读取成功*/

        /*打印到终端*/
        printf("%s\n",buf);
    }

    return 0;
}

5_write.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
    int fd;        //文件描述符
    int ret;
    char buf[100] = {0};

    /*打开一个文件,以读的方式*/
    fd = open("test.txt",O_RDONLY);
    if(fd < 0){
        perror("open test");
        return -1;
    }
    
    /*把文件指针移动到文件头*/
    lseek(fd,0,SEEK_SET);

    /*读取标准终端 : 0 (STDIN_FILENO)*/
    ret = read(fd,buf,sizeof(buf)-1);
    if(ret > 0){
        /*读取成功*/

        /*打印到终端*/
        write(1,buf,ret);
    }

    return 0;
}

6_write_read.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
    int fdr;        //文件描述符
    int fdw;
    int ret;
    char buf[100] = {0};

    /*打开一个文件,以读的方式*/
    fdr = open("test.txt",O_RDONLY);
    if(fdr < 0){
        perror("open test");
        return -1;
    }
    
    /*打开一个文件dest.txt,用于写,如果文件不存在,则创建*/
    fdw = open("dest.txt",O_WRONLY | O_CREAT,0755);
    if(fdw < 0){
        perror("open dest");
        return -1;
    }

    /*把文件指针移动到文件头*/
    lseek(fdr,0,SEEK_SET);
    
    /*读取标准终端 : 0 (STDIN_FILENO)*/
    ret = read(fdr,buf,sizeof(buf)-1);
    if(ret > 0){
        /*读取成功*/

        /*写入到文件dest.txt*/
        write(fdw,buf,ret);
    }

    return 0;
}

 

文件I/O的操作实例

标签:

原文地址:http://www.cnblogs.com/yihujiu/p/5520976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!