fopen & fcolse & fseek & ftell & fstat 文件操作函数测试
1、文件大小查询file_size.c
方法一:fseek + ftell;
方法二:ftell
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> void errHandling(const char *errMsg)
{
printf("%s: %s\n", errMsg, strerror(errno));
exit(-);
} long getFileSize1(FILE *fp)
{
if (fseek(fp, , SEEK_END) != )
{
errHandling("fseek() fail");
}
return ftell(fp);
} long getFileSize2(int fd)
{
struct stat st;
if ((fstat(fd, &st)) != )
{
errHandling("fstat() fail");
}
return st.st_size;
} int main(int argc, char *argv[])
{
if (argc != )
{
printf("Usage: %s <file_name>\n", argv[]);
exit(-);
} FILE *fp = fopen(argv[], "r");
if (NULL == fp)
{
errHandling("open() fail");
} printf("The size of %s: %ld bytes (fseek+ftell)\n", argv[], getFileSize1(fp));
printf("The size of %s: %ld bytes (fstat)\n", argv[], getFileSize2(fileno(fp))); fclose(fp);
exit();
}
2、特定大小文件创建以及读取操作时间测试 read_file_time.c
描述:创建1G大小文件,并完成顺序、逆序以及随机读取操作
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h> #define BUF_SIZE (1024 * 1024)
#define READ_SIZE (BUF_SIZE * 1024)
#define NUM_ROUND 1024 void errHandling(const char *errMsg)
{
printf("%s: %s\n", errMsg, strerror(errno));
exit(-);
} long getFileSize(int fd)
{
struct stat st;
if ((fstat(fd, &st)) != )
{
errHandling("fstat() fail");
}
return st.st_size;
} /* in sequence */
unsigned long getReadTimeSeq(char *pbuf, FILE *pf)
{
int readCnt = ;
struct timeval bgn;
struct timeval end;
unsigned long timeCnt = ; memset(&bgn, , sizeof(struct timeval));
memset(&end, , sizeof(struct timeval));
printf("Start read in sequence\n");
gettimeofday(&bgn, NULL);
while (readCnt < READ_SIZE)
{
memset(pbuf, , BUF_SIZE);
readCnt += fread(pbuf, , BUF_SIZE, pf);
//printf("read %d MB\n", readCnt >> 20);
}
gettimeofday(&end, NULL);
return ((end.tv_sec - bgn.tv_sec) * + (end.tv_usec - bgn.tv_usec));
} /* inverted sequence */
unsigned long getReadTimeInvertSeq(char *pbuf, FILE *pf)
{
int readCnt = ;
long shift = READ_SIZE - BUF_SIZE;
struct timeval bgn;
struct timeval end;
unsigned long timeCnt = ; memset(&bgn, , sizeof(struct timeval));
memset(&end, , sizeof(struct timeval));
//printf("Start read in inverted sequence\n");
gettimeofday(&bgn, NULL);
while (readCnt < READ_SIZE)
{
fseek(pf, shift, SEEK_SET);
memset(pbuf, , BUF_SIZE);
readCnt += fread(pbuf, , BUF_SIZE, pf);
shift -= readCnt;
//printf("read %d MB\n", readCnt >> 20);
}
gettimeofday(&end, NULL);
return ((end.tv_sec - bgn.tv_sec) * + (end.tv_usec - bgn.tv_usec));
} /* Random sequence */
unsigned long getReadTimeRandPos(char *pbuf, FILE *pf)
{
int readCnt = ;
long shift = READ_SIZE - BUF_SIZE;
struct timeval bgn;
struct timeval end;
unsigned long timeCnt = ; memset(&bgn, , sizeof(struct timeval));
memset(&end, , sizeof(struct timeval));
srand((int)time());
//int num = 0;
gettimeofday(&bgn, NULL);
while (readCnt < READ_SIZE)
{
//++num;
shift = BUF_SIZE * (rand() % NUM_ROUND);
fseek(pf, shift, SEEK_SET);
memset(pbuf, , BUF_SIZE);
readCnt += fread(pbuf, , BUF_SIZE, pf);
}
gettimeofday(&end, NULL);
//printf("num = %d\n", num);
return ((end.tv_sec - bgn.tv_sec) * + (end.tv_usec - bgn.tv_usec));
} int main(int argc, char *argv[])
{
if (argc != )
{
printf("Usage: %s <file_name>\n", argv[]);
exit(-);
} FILE *pf = fopen(argv[], "w+");
if (NULL == pf)
{
errHandling("open() fail");
}
/*生成大小为1G的文件*/
fseek(pf, READ_SIZE, SEEK_SET);
fputc(, pf);
rewind(pf); char *buf = (char *)malloc(BUF_SIZE * sizeof(char));
if (NULL == buf)
{
errHandling("malloc() fail");
} printf("Time in sequence: timeCnt = %ld us\n", getReadTimeSeq(buf, pf));
printf("Time in inverted sequence: timeCnt = %ld us\n", getReadTimeInvertSeq(buf, pf));
printf("Time in random sequence: timeCnt = %ld us\n", getReadTimeRandPos(buf, pf)); fclose(pf);
free(buf);
exit();
}
3、编译
EXES := read size .PHONY : all
all : $(EXES) read : read_file_time.o
gcc -o read read_file_time.o
read_file_time.o : read_file_time.c
gcc -c read_file_time.c size : file_size.o
gcc -o size file_size.o
file_size.o : file_size.c
gcc -c file_size.c clean :
rm -f *.o $(EXES)
统一编译以上两个源文件,并生成两个对应的可执行文件
fopen & fcolse & fseek & ftell & fstat 文件操作函数测试的更多相关文章
- C语言样式的文件操作函数
使用C语言样式的文件操作函数,需要包含stdio.h头文件. 1.打开文件的函数: //oflag的取值为“w”或“r”,分别表示以写或读的方式打开 FILE* fd = fopen(filename ...
- C语言文件操作函数
C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * str ...
- 【阅读笔记】《C程序员 从校园到职场》第六章 常用文件操作函数 (Part 1)
参考链接:https://blog.csdn.net/zhouzhaoxiong1227/article/details/24926023 让你提前认识软件开发(18):C语言中常用的文件操作函数总结 ...
- Linux C 文件操作函数(~上善止水~)
翻翻笔记,整理一下 C 语言中的文件操作函数 ~~~~~~,多注意细节,maybe 细节决定成败~ 1. fopen /* fopen(打开文件) * * 相关函数 open,fclose * * 表 ...
- 文件操作(FILE)与常用文件操作函数
文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...
- C语言文件操作函数大全(超详细)
C语言文件操作函数大全(超详细) 作者: 字体:[增加 减小] 类型:转载 本篇文章是对C语言中的文件操作函数进行了详细的总结分析,需要的朋友参考下 fopen(打开文件)相关函数 open,fc ...
- PHP常用的文件操作函数集锦
以下是个人总结的PHP文件操作函数.当然,这只是部分,还有很多,我没有列出来. 一 .解析路径: 1 获得文件名:basename();给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件 ...
- 总结文件操作函数(一)-C语言
在进程一開始执行,就自己主动打开了三个相应设备的文件.它们是标准输入.输出.错误流.分别用全局文件指针stdin.stdout.stderr表示,相应的文件描写叙述符为0.1.2:stdin具有可读属 ...
- 总结文件操作函数(二)-C语言
格式化读写: #include <stdio.h> int printf(const char *format, ...); //相当于fprintf( ...
随机推荐
- 【转】Qt在pro中设置运行时库MT、MTd、MD、MDd,只适合VS版本的Qt
http://blog.csdn.net/caoshangpa/article/details/51416077 一.在pro中设置运行时库 最近在用Qt5.6.0(VS2013版本)调用一份用Vis ...
- Unix网络编程_卷1卷2
1. UNIX 网络编程(第2版)第1卷:套接口API和X/Open 传输接口API PDFhttp://www.linuxidc.com/Linux/2014-04/100155.htm UNIX网 ...
- HDU 4489 The King's Ups and Downs
HDU 4489 The King's Ups and Downs 思路: 状态:dp[i]表示i个数的方案数. 转移方程:dp[n]=∑dp[j-1]/2*dp[n-j]/2*C(n-1,j-1). ...
- typescripts学习
可选与默认参数 可选参数:在参数名后面,冒号前面添加一个问号,则表明该参数是可选的.如下代码: function buildName(firstName: string, lastName?: str ...
- 递归C++
递归C++ 一.递归简介 自己调用自己 二.递归写法 2.1 写法介绍 先写出问题的递推公式 递归部分的边界条件就是递推公式中的边界条件 递归部分的主体部分就是递推公式中的主体部分 2.2 实例 (1 ...
- JSP中scope属性 scope属性决定了JavaBean对象存在的范围
scope属性决定了JavaBean对象存在的范围. scope的可选值包括: ---page(默认值) ---request ---session ---application 1.page范围 ...
- English trip -- VC(情景课)10 A Get ready 预备课
Words dance 跳舞 exercise 运动:锻炼 fish 鱼 play basketball 打篮球 play cards 玩牌 swim 游泳 decorations 装饰品 ...
- fzu1901 kmp
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...
- 现在转战c++的领域,纯幼儿园
C++中: 如果你用#include<iostream.h>就不需写这句话(旧标准).但是如果你用#include<iostream>就必须要写.但是在VS2010中就出现错误 ...
- 快速排序的C++版
int Partition(int a[], int low, int high) { int x = a[high];//将输入数组的最后一个数作为主元,用它来对数组进行划分 int i = low ...