Linux下的文件操作

API:应用程序接口

1.创建一个文件:

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

int main()
{
    int fd; //文件描述符
    fd = creat("hello.c", S_IRUSR | S_IWUSR); //用户可读可写
    if(-1 == fd)
    {
        perror("creat");
        exit(1);
    }
}

2.打开一个文件:

 

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

int main()
{
    int fd; //文件描述符
    int ret;
    char buf[32] = {0};

    fd = open("hello.txt", O_CREAT | O_RDWR | O_EXCL, 00700); //文件必须不存在
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }
    
    scanf("%s", buf);

    ret = write(fd, buf, strlen(buf));//把buf的内容写到fd里面
    if(-1 == ret)
    {
        perror("write");
        exit(1);
    }
    
    //文件指针的三种方式
    lseek(fd, 0, SEEK_SET);//文件指针相对于文件开头移动0个字节
    lseek(fd, strlen(buf) * -1, SEEK_CUR);//文件指针相对于当前位置向前移动xx个位置
    lseek(fd, strlen(buf) * -1, SEEK_END);//文件指针相对于最后的位置向前移动xx个位置
    
    memset(buf, 0, sizeof(buf));//清空buf的缓冲区

    ret = read(fd, buf, sizeof(buf));//把fd里的内容写到buf里面
    if(-1 == ret)
    {
        perror("read");
        exit(1);
    }

    printf("%s\n", buf);

    close(fd);

    return 0;
}

 

3.如何拷贝一个文件:

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

int main(int argc, char *argv[])//命令行参数
{
    if(argc != 3)
    {
        perror("usage: ./copy hello.c xxx.c");
        exit(1);
    }

    int fd_from, fd_to;
    int ret;
    char buf[32] = {0};

    fd_from = open(argv[1], O_RDONLY);   //打开目标文件 
    if(-1 == fd_from)
    {
        perror("open");
        exit(1);
    }

    fd_to = open(argv[2], O_CREAT | O_WRONLY | O_EXCL, 00700); //创建并打开一个文件
    if(-1 == fd_to)
    {
        perror("open");
        exit(1);
    }

    while(1)//不停的从第一个文件里读,再不停的向第二个文件里写
    {
        ret = read(fd_from, buf, sizeof(buf) - 1);
        if(-1 == ret)
        {
            perror("read");
            exit(1);
        }

        else if(0 == ret)
        {
            printf("copy success!\n");
            break;
        }

        ret = write(fd_to, buf, ret);

        memset(buf, 0, sizeof(buf));
    }

    close(fd_from);
    close(fd_to);

    return 0;
}

4.C库里面的文件操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *fp; //定义一个文件指针
    int ret;
    char buf[32] = "helloworld";

    fp = fopen("hello.txt", "a+");
//r,rb :        只读方式打开,文件必须已存在
  w,wb :        只写方式打开,如果文件不存在则创建,如果文件已存在清空重写
  a, ab:        只能在文件末尾追加数据,如果文件不存在则创建
  r+,rb+,r+b:  读写方式打开,文件必须已存在
  w+,w+b,wb+:  读写方式打开,如果文件不存在则创建,如果文件已存在清空重写
  a+,a+b,ab+:   读和追加方式打开,如果文件不存在则创建
    
    if(NULL == fp)
    {
        perror("fopen");
        exit(1);
    }

    ret = fwrite(buf, 1, strlen(buf), fp); //分配1*strlen(buf)个字节到fp,将buf的内容写到fp
    if(0 == ret)
    {
        perror("fwrite");
        exit(1);
    }

    fleek(fp, 0, SEEK_SET);
    
    memset(buf, 0, sizeof(buf));

    ret = fread(buf, 1, strlen(buf), fp);
    if(0 == ret)
    {
        perror("fread");
        exit(1);
    }

    printf("%s\n", buf);

    return 0;
}

5.计算一个文件的长度

 

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

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("usage: ./length hello.c");
    }
    int fd, length;
    
    fd = open(argv[1], O_RDWR);
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }
    
    length = lseek(fd, 0, SEEK_END);

    printf("%d\n", length);

    //
    fseek(fp, 0, SEEK_END);
    length = ftell(fp);//求文件大小的函数
    //

    return 0;
}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值