嵌入式Linux标准IO,获取文件大小fgetc(),定位流获取文件大小fteel()、rewind()/fseek(),处理错误信息perror()/strerror()
2018-11-01 00:12
337 查看
#include <stdio.h> #include <errno.h> #include <string.h> int get_file_size(const char *file); int main(int argc, const char *argv[]) { if(argc < 2) { printf("no file name or path\n"); return -1; } else { printf("total %d bytes\n",get_file_size(argv[1])); } return 0; }
1、fgetc()获取文件大小
int get_file_size(const char *file) { int count=0; FILE *fp; if((fp = fopen(file,"r")) == NULL) { perror("fopen"); //printf("fopen:%s\n",strerror(errno));//errno-----<errno.h>,strerror()------<string.h> return -1; } while(fgetc(fp) != EOF) { count ++; } if((fclose(fp)) == EOF) { perror("fclose"); return EOF; } return count; }
运行结果
2、定位流获取文件大小fseek(),ftell()
int get_file_size(const char *file) { int count; FILE *fp; if((fp = fopen(file,"r")) == NULL)//文件以只读模式打开时流的读写位置在文件开头。若打开模式是“a”追加,则读写位置在文件末尾 { perror("fopen"); return -1; } fseek(fp,0,SEEK_END);//将流的读写位置定位到文件末尾 count = ftell(fp);//读取流的当前读写位置 if((fclose(fp)) == EOF) { perror("fclose"); return EOF; } return count; }
运行结果
阅读更多
相关文章推荐
- 嵌入式Linux标准IO,fopen(),fclose(),错误信息处理errno,perror(),strerror()
- 嵌入式Linux标准IO,拷贝文件fgetc()/fputc(),fread()/fwrite()
- 嵌入式Linux标准IO,刷新流fflush(),流定位ftell()/fseek()/rewind(),流出错和结束ferror()/feof()
- 嵌入式Linux标准IO,获取文件行数
- Linux系统编程之错误处理:perror,strerror和errno
- linux系统编程之错误处理:perror,strerror和errno
- 关于Linux系统编程的错误处理:errno、perror、strerror
- 嵌入式Linux文件IO,标准IO和文件IO的区别,文件描述符,open()函数,close()函数
- 嵌入式Linux文件IO,GCC编译流程及参数分析,静态库创建和使用,ar crs libhello.a hello.o创建静态库,nm查看静态库中符号信息
- VirtualBox的Ubuntu中文件共享问题的解决:未处理的错误消息,获取文件"/media/sf_***"信息出错,协议错误
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- 嵌入式Linux标准IO,time(),localtime(),sleep(),fprintf(),每隔1秒向文件test.txt中写如当前系统时间
- linux系统编程之文件与IO:stat()系统调用获取文件信息
- Get and display the size of file and directory in Linux system using du command 获取和现实linux文件大小(三)
- linux下 stat statfs 获取 文件 磁盘 信息
- Linux程序设计-获取错误信息
- Linux下获取配置文件信息
- linux系统编程之错误处理:perror,strerror和errno
- 【整理】将Linux脚本中的正常输出,警告,错误等信息输出到文件中
- 说明: 在处理向该请求提供服务所需的配置文件时出错。请检查下面的特定错误详细信息并适当地修改配置文件。 分析器错误信息: 无法识别的属性“type”。