C语言文件操作 stat,fseek,copy

stat()

  • 头文件:#include
  • 定义函数:int stat(const char * file_name, struct stat *buf);
  • 说明:用来将参数file_name 所指的文件状态, 复制到参数buf 所指的结构中。
  • 返回值:执行成功则返回0,失败返回-1,错误代码存于errno。
struct stat {
    mode_t st_mode; //(文件保护模式)文件类型和权限信息 结构体详解请参考此处
    ino_t st_ino; //文件结点号
    dev_t st_dev; //文件所在设备的文件系统标识号 device number (file system)
    dev_t st_rdev; //文件所表示的特殊设备文件的设备标识 device number for special files
    short st_nlink; //符号链接数
    short st_uid; //文件用户标识 用户ID
    short st_gid; //文件用户组标识 组ID
    off_t st_size; // 总大小,字节为单位 size in bytes,for regular files
    time_t st_st_atime; //文件内容最后访问的时间
    time_t st_mtime; //文件内容最后修改时间
    time_t st_ctime; //文件结构最后状态改变时间
};

文件复制

  • 使用栈内存缓冲区

  • 是用堆内存缓冲,并一次读写

    #include 
    #include 
    #include 
    #include 
    
    char *pathSource = "E:\\CFile\\source.mp4";
    char *pathTarget = "E:\\CFile\\target.mp4";
    
    long getFileSize(const char *);
    
    int main() {
        clock_t start, end;
        //该函数返回自程序启动起,处理器时钟所使用的时间。如果失败
        start = clock();
        long size = getFileSize(pathSource);
        printf("%s size is %ld\n", pathSource, size);
        FILE *fileSource = fopen(pathSource, "rb");
        FILE *fileTarget = fopen(pathTarget, "wb");
        //栈内存中做缓冲,多次读写
    //    char buf[1024] = {0};
    //    while (!feof(fileSource)) {
    //        int read = fread(buf, sizeof(char), sizeof(buf), fileSource);
    //        if (read > 0)
    //            fwrite(buf, sizeof(char), read, fileTarget);
    //    }
        //堆内存中做缓冲,一次读写。
        char *buf = malloc(size);
        fread(buf, size, 1, fileSource);
        fwrite(buf, size, 1, fileTarget);
        fclose(fileSource);
        fclose(fileTarget);
        free(buf);
        end = clock();
        printf("spend time : %d\n", end - start);
        printf("%s size is %ld\n", pathTarget, size);
        return 0;
    }
    
    long getFileSize(const char *path) {
        struct stat fileStat;
        //获取文件信息和状态
        stat(path, &fileStat);
        return fileStat.st_size;
    }
    
    
C语言文件操作 stat,fseek,copy_第1张图片
2.png

fseek()

  • 定义 int fseek(FILE *stream, long int offset, int whence)
  • stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。
  • offset -- 这是相对 whence 的偏移量,以字节为单位。
  • whence -- 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一
  • 如果成功,则该函数返回零,否则返回非零值。
常量 描述
SEEK_SET 文件的开头
SEEK_CUR 文件指针的当前位置
SEEK_END 文件的末尾

作者:
链接:http://www.jianshu.com/p/q81RER
來源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 示例代码

    #include 
    
    char *path = "E:\\CFile\\1.txt";
    
    int main() {
        FILE *file = fopen(path, "r");
        char buf[30] = {0};
        //测试 1
        fseek(file, 2, SEEK_SET);
        //测试 2
    //    fread(buf, sizeof(char), 3, file);
    //    fseek(file, -2, SEEK_CUR);
        //测试 3
    //    fseek(file, -2, SEEK_END);
        while (!feof(file)) {
            int count = fread(buf, sizeof(char), 30, file);
            if (count > 0) {
                printf("%s", buf);
            }
        }
        fclose(file);
        return 0;
    }
    

    "E:\CFile\1.txt"的文件内容为:

    abcdefg
    1234567
    

    测试1的代码运行,文件指针从文件头开始往后偏移2个字节,结果为

    cdefg
    1234567
    

    测试2的代码运行,文件先读3个字节,然后从当前位置往前偏移两个字节,结果为:

    bcdefg
    1234567
    

    测试3的代码运行,文件指针从文件末尾往前偏移两个字节,结果为:

    67
    

文件读写结构体

  • 文件写入结构体,示例代码:

    #include 
    
    char *path = "E:\\CFile\\person.txt";
    
    struct Person {
        char name[10];
        int number;
    };
    
    int main() {
        struct Person person[3] =
                {
                        {"华仔",   1},
                        {"jack", 2},
                        {0},
                };
        FILE *file = fopen(path, "wb");
        fwrite(person, sizeof(struct Person), 3, file);
        fclose(file);
        return 0;
    }
    
  • 查看写入文件的内容:


    3.png
    • 首先看到结构体的内存对齐,一个Person占用4个字节。同时int存储为小端对齐。
    • 存储的内容也符合预期,与初始化的结构体一致。
  • 结构体文件读

    int read() {
        FILE *file = fopen(path, "rb");
        struct Person person[2] = {0};
        fread(person, sizeof(struct Person), 2, file);
        for (int i = 0; i < 2; i++) {
            printf("name=%s,number=%d", person[i].name, person[i].number);
        }
        fclose(file);
        return 0;
    }
    

你可能感兴趣的:(C语言文件操作 stat,fseek,copy)