文件IO 例子

例子1: 测试最多打开多少个文件

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

int main(int argc, const char *argv[])
{
    int fd;
    int cnt = 0;
    while(1) //循环打开
    {
        if((fd=open(argv[1],O_RDWR))<0) //打开文件返回文件描述符, 0,1,2 是系统开启进程自动分配的 标准输入,输出,出错
            break;
        cnt++; //正确打开文件,计数加1
        printf("fd=%d\n",fd);
    }
    printf("cnt = %d\n",cnt);

    close(fd);//关闭文件

    return 0;
}

测试: 运行程序 ./a.out tt.txt > file.txt    ------  tt.txt 是程序中argv[1] 打开的文件, > 表示重定向,把内容输入到文件file.txt中,在终端显示会很多内容

例子2; 关于文件描述符

 例子3 : read的使用

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


int main(int argc, const char *argv[])
{
    char buf[20] = {0};
#if 0 
    //从终端读取数据
    read(0,buf,20);
    printf("buf = %s\n",buf);
#endif

    //从文件读取数据
    int fd;
    fd = open(argv[1],O_RDONLY);
    if(fd < 0)
    {
        perror("open fail : ");
        return -1;
    }
    ssize_t bytes;
    int i = 0;
    bytes = read(fd,buf,10); //读取fd文件内容放到buf中,每次读取10个,返回读取到的个数
    printf("bytes = %d->",bytes);
    printf("buf = [%s]\n",buf);
    for(i=0;i<10;i++)  //没次读完把buf清空防止影响下次使用
        buf[i] = 0;

    bytes = read(fd,buf,10);
    printf("bytes = %d->",bytes);
    printf("buf = [%s]\n",buf);
    
    for(i=0;i<10;i++)
        buf[i] = 0;

    bytes = read(fd,buf,10);
    printf("bytes = %d->",bytes);
    printf("buf = [%s]\n",buf);
    for(i=0;i<10;i++)
        buf[i] = 0;

    close(fd);
    return 0;
}

测试

 例子4 :write 写数据

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
    char buf[20] = "hello world!\n";
#if 0
    //写数据到终端
    write(1,buf,20);
#endif
#if 1
    //写数据,到文件
    int fd;
    fd = open(argv[1],O_WRONLY|O_CREAT|O_TRUNC);//只读方式打开,没有创建,打开清空文件
    if(fd < 0)
    {
        perror("open fail : ");
        return -1;
    }
    write(fd,buf,20);//把buf的内容写到fd指向的文件中,写入20个字符
    close(fd);
#endif
    return 0;
}

测试

 例子5 :read和write实现文件内容复制

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

int main(int argc, const char *argv[])
{
    char buf[20] = "hello world!";

    //写数据,到文件
    int fd_r,fd_w;
    fd_r = open(argv[1],O_RDONLY);  //只读方式打开
    if(fd_r < 0)
    {
        perror("open fail : ");
        return -1;
    }

    fd_w = open(argv[2],O_WRONLY);  //只写方式打开
    if(fd_w < 0)
    {
        perror("open fail : ");
        return -1;
    }

    int read_len = 0;
    while(1)
    {
        read_len = read(fd_r,buf,10);
        if(read_len == 0)//当读取到0个字符时,即为读到文件末尾,出错发挥-1
        {
            break;
        }
        write(fd_w,buf,read_len);//把读取到的长度是写入另外的文件长度
    }

    close(fd_r);
    close(fd_w);
    return 0;
}

测试

 例子6 : lseek文件偏移量

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

int main(int argc, const char *argv[])
{
    char buf[] = "hello world!";

    //写数据,到文件
    int fd_r;
    fd_r = open(argv[1],O_RDWR | O_TRUNC,0666);  //读写方式打开,清空打开
    if(fd_r < 0)
    {
        perror("open fail : ");
        return -1;
    }
    write(fd_r,buf,sizeof(buf));//写数据到文件
    
    off_t off;
    off = lseek(fd_r,0,SEEK_CUR);//获取当前偏移量
    printf("当前 off = %ld\n",off);

    off = lseek(fd_r,3,SEEK_SET); //在文件起始位置偏移 3
    printf("SEEK_SET off = %ld\n",off);

    write(fd_r,"123",3);//偏移后向文件中写入数据

    lseek(fd_r,-5,SEEK_END); // 在文件末尾向前偏移 5 在写入数据
    write(fd_r,"ABCD",4);

    close(fd_r);
    return 0;
}

测试:       fd_r = open(argv[1],O_RDWR | O_APPEND,0666); //读写方式打开,追加打开,设置了偏移量也只能在文件末尾添加

猜你喜欢

转载自www.cnblogs.com/electronic/p/10914707.html