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 文件操作函数测试的更多相关文章

  1. C语言样式的文件操作函数

    使用C语言样式的文件操作函数,需要包含stdio.h头文件. 1.打开文件的函数: //oflag的取值为“w”或“r”,分别表示以写或读的方式打开 FILE* fd = fopen(filename ...

  2. C语言文件操作函数

    C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * str ...

  3. 【阅读笔记】《C程序员 从校园到职场》第六章 常用文件操作函数 (Part 1)

    参考链接:https://blog.csdn.net/zhouzhaoxiong1227/article/details/24926023 让你提前认识软件开发(18):C语言中常用的文件操作函数总结 ...

  4. Linux C 文件操作函数(~上善止水~)

    翻翻笔记,整理一下 C 语言中的文件操作函数 ~~~~~~,多注意细节,maybe 细节决定成败~ 1. fopen /* fopen(打开文件) * * 相关函数 open,fclose * * 表 ...

  5. 文件操作(FILE)与常用文件操作函数

    文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...

  6. C语言文件操作函数大全(超详细)

    C语言文件操作函数大全(超详细) 作者: 字体:[增加 减小] 类型:转载 本篇文章是对C语言中的文件操作函数进行了详细的总结分析,需要的朋友参考下   fopen(打开文件)相关函数 open,fc ...

  7. PHP常用的文件操作函数集锦

    以下是个人总结的PHP文件操作函数.当然,这只是部分,还有很多,我没有列出来. 一 .解析路径: 1 获得文件名:basename();给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件 ...

  8. 总结文件操作函数(一)-C语言

    在进程一開始执行,就自己主动打开了三个相应设备的文件.它们是标准输入.输出.错误流.分别用全局文件指针stdin.stdout.stderr表示,相应的文件描写叙述符为0.1.2:stdin具有可读属 ...

  9. 总结文件操作函数(二)-C语言

    格式化读写: #include <stdio.h> int printf(const char *format, ...);                   //相当于fprintf( ...

随机推荐

  1. 51nod 1463 找朋友(线段树+离线处理)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1463 题意: 思路: 好题! 先对所有查询进行离线处理,按照右区间排序, ...

  2. Python中浮点数精度处理

    Python中,浮点数运算,经常会碰到如下情况: 出现上面的情况,主要还是因浮点数在计算机中实际是以二进制保存的,有些数不精确.比如说: 0.1是十进制,转化为二进制后它是个无限循环的数:0.0001 ...

  3. webstorm注册码 永久有效!!! 前端工程师福利

    2RRJMBXW33-eyJsaWNlbnNlSWQiOiIyUlJKTUJYVzMzIiwibGljZW5zZWVOYW1lIjoi5b285bK4IHNvZnR3YXJlMiIsImFzc2lnb ...

  4. Spring AMQP 源码分析 02 - CachingConnectionFactory

    ### 准备 ## 目标 了解 CachingConnectionFactory 在默认缓存模式下的工作原理   ## 前置知识   <Spring AMQP 源码分析 01 - Impatie ...

  5. ArcGIS 要素闪烁

    ArcGIS 要素闪烁 通过双击要素图例,闪烁定位到要素,并且闪烁一段时间: IFeatureLayer featureLayer = layer as IFeatureLayer;          ...

  6. English trip -- VC(情景课) 8 B job duties 工作职责

    Vocabulary focus  核心词汇 She is a receptionist. She answers the phone. She is a cashier She counts mon ...

  7. Confluence 6 为 Active Directory 配置一个 SSL 连接

    如果你希望配置 Microsoft Active Directory 的读写权限,你需要在你的 Confluence 服务器和JVM keystore 上安装 Active Directory 服务器 ...

  8. 牛客网暑期ACM多校训练营(第一场)I Substring

    题意:给你一个只有abc的字符串,求不相同的子串,(不同构算不同,例如aba和bab算同构) 题解:很显然,如果不考虑同构的问题,我们直接上sa/sam即可,但是这里不行,我们考虑到只有abc三种字符 ...

  9. quick pow

    #include<iostream> using namespace std; #define LL long long LL qpow(LL a,LL b,LL m) { LL r=1; ...

  10. 第5章——使用 Razor(MVC框架视图引擎)

    Razor 是MVC框架视图引擎的名称. 本章提供 Razor 语法的快速教程,以使你能够识别 Razor 表达式. 本章不打算提供 Razor 的完整参考,而将其视为一个语法速成教程.在本书的后续内 ...