2025年3月29日 星期六 甲辰(龙)年 月廿八 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > C语言

获取文件大小以及读写文件操作

时间:03-30来源:作者:点击数:39

获取文件大小:

  • int getFileLength(const char* fileName)
  • {
  • int length = 0;
  • FILE *file;
  • file = fopen(fileName, "r");
  • if (file == NULL)
  • {
  • return -1;
  • }
  • fseek(file, 0L, SEEK_END);
  • length = ftell(file);
  • fclose(file);
  • return length;
  • }

已知文件大小读文件:

  • int getFileData(const char* fileName, unsigned char *data, int length)
  • {
  • FILE *file;
  • file = fopen(fileName, "r");
  • if (file == NULL)
  • {
  • return -1;
  • }
  • int ret = fread(data, 1, length, file);
  • fclose(file);
  • return 0;
  • }

已知文件大小写文件:

  • int writeFile(const char* fileName, unsigned char *data, int size)
  • {
  • FILE *file;
  • file = fopen(fileName, "wb");
  • if (file == NULL)
  • {
  • return -1;
  • }
  • int ret = fwrite(data, 1, size, file);
  • fclose(file);
  • return 0;
  • }
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门