前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言文件操作 stat,fseek,copy

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

作者头像
用户2929716
发布2018-08-23 13:13:49
1.3K0
发布2018-08-23 13:13:49
举报
文章被收录于专栏:流媒体

stat()

  • 头文件:#include<sys/stat.h>
  • 定义函数:int stat(const char * file_name, struct stat *buf);
  • 说明:用来将参数file_name 所指的文件状态, 复制到参数buf 所指的结构中。
  • 返回值:执行成功则返回0,失败返回-1,错误代码存于errno。
代码语言:javascript
复制
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 <stdio.h> #include <sys/stat.h> #include <malloc.h> #include <time.h> 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; }

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 <stdio.h> 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 <stdio.h> 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; }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.07.14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • stat()
  • 文件复制
  • fseek()
  • 文件读写结构体
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档