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. asp.net <asp:Repeater>下的 asp:LinkButton CommandArgument点击事件

    前台 <asp:Repeater ID="rptData" runat="server" OnItemCommand="rptData_Item ...

  2. 51nod 1042 数字0-9的数量 数位dp

    1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 给出一段区间a-b,统计这个区间内0-9出现的次数.   比如 10-1 ...

  3. codeforces 352 div 2 C.Recycling Bottles 贪心

    C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Goroutines和Channels(一)

    Go语言中的并发程序可以用两种手段来实现.本章讲解goroutine和channel,其支持“顺序通信进程”(communicating sequential processes)或被简称为CSP.C ...

  5. shell 函数调用

    例一 #!/bin/bash create_link() { filelist=`ls $` for file in $filelist do echo $/$file done } create_l ...

  6. myEclipse 下配置多个Tomcat

    1.进入perfomance 2. 进入server  右键点击configure server connector 3. 切换到 “Arguments” 面板,这里有 一个启动参数,就是修改一下路径 ...

  7. .net Parallel并行使用

    因项目响应过慢,代码优化空间不大,在暂时无法调整系统架构的情况下,只有使用.NET中的TPL解决一些模块耗时过多的问题.但在使用过程中也碰到了一些问题,现在把它写下来,用于备忘. 1. Paralle ...

  8. Python day17 模块介绍1(time,random)

    module模块和包的介绍(略掉了) 常用模块 # time模块 import time print(time.time())#时间戳,在1970年开始到现在一共多少秒 print(time.gmti ...

  9. Qt532_字符编码转换

    1.测试代码: // http://blog.csdn.net/changsheng230/article/details/6588447 QString str = QString::fromLoc ...

  10. 编写自己的代码库(javascript常用实例的实现与封装)[转]

    1.前言 因为公司最近项目比较忙,没那么多空余的事件写文章了,所以这篇文章晚了几天发布.但是这也没什么关系,不过该来的,总是会来的.好了,其他的不多说的,大家在开发的时候应该知道,有很多常见的实例操作 ...