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. navicat Window . MAC版常用快捷键

    navicat 结合快捷键 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shi ...

  2. Python CSV Reader/Writer 例子--转载

    CSV(comma-separated values) 是跨多种形式导入导出数据的标准格式,比如 MySQL.Excel. 它以纯文本存储数和文本.文件的每一行就代表一条数据,每条记录包含了由逗号分隔 ...

  3. miRNA几大常用的数据库

    1.miRbasehttp://www.mirbase.org/2.miRDBhttp://www.mirdb.org/miRDB/policy.html3.miRandahttp://www.mic ...

  4. React Native 的组件之底部导航栏 TabBarIOS(一)

    import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, TabBarIOS, } fro ...

  5. python 判断字符串是否以数字结尾

    import re def end_num(string): #以一个数字结尾字符串 text = re.compile(r".*[0-9]$") if text.match(st ...

  6. [ios][switf]页面跳转

    参考:http://bbs.csdn.net/topics/390899712 注意用push会崩溃 用其他的正常 1.storyboard直接拖拉,使用不同种类的segue均可2.直接写代码: // ...

  7. wacher和acl

      一.wacher    问题      1.集群中有多个机器,当某个通用的配置发生变化 ,怎么让所有服务器的配置都统一生效?       2.当某个集群节点宕机,其它节点怎么知道?   Zk中引入 ...

  8. 《图解Http》 HTTPS 安全协议

    相关博客:https://www.cnblogs.com/chentianwei/p/9374341.html  (讲的更明白,有图) HTTPS 7.1http的缺点 使用明文,内容会被窃听. 不验 ...

  9. codeforces 494a//Treasure// Codeforces Round #282(Div. 1)

    题意:一个'('  ,  ')'  ,  '#'组成的串,可将'#'换成至少一个')'.问一个换法能使串匹配. 至少换成一个,那么就先都换成一个,记结果为str.最后一个')'的后面没有未匹配的'(' ...

  10. 求逆序数的方法--线段树法&归并排序法

    逆序数的概念:对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,就说有1个逆 ...