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( ...
随机推荐
- python argparse模块--转载
add_argument:读入命令行参数,该调用有多个参数 ArgumentParser.add_argument(name or flags…[, action][, nargs][, const] ...
- Linux——文件搜索命令简单笔记
一: 命令名称:which 命令所在路径:/usr/bin/which 执行权限:所有用户 功能描述:显示系统命令所在目录 范例:$ which ls 还有一个whereeis ls 命令 二: 命令 ...
- [ios]"The identity used to sign the executable is no longer valid"错误解决方法
重新去开发者网站,申请一遍profiles 参考:http://www.bubuko.com/infodetail-982908.html 我出现这个错误的情况,程序提交app store之后,第二天 ...
- sublime text注册码与快捷键
其他版本: —– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 ...
- php 获取自己的公网IP
<?php $externalContent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/Current IP ...
- LeetCode--100--相同的树
问题描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2, ...
- codeforces 1041d// Glider// Codeforces Round #509(Div. 2)
题意:给出,n和飞行员高度h,n是区间数.在区间里飞行员高度不变,其它地方每秒高度-1,x坐标+1.问在高度变为0以前,x坐标最多加多少? 用数组gap记录本区间右端到下一个区间左端的距离.用sum记 ...
- CoderForce 180D-Name (构造+回溯)
题目大意:给两个字符串s,t,用s中的字符重新组合构造出按字典序最小的但比t大的新字符串. 题目分析:先统计s中各个字母出现的次数,然后从t的左端向右端依次构造出新串的每一位上的字母.这个过程我是用回 ...
- HDU-1163 Eddy's digital Roots(九余数定理)
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 在Eclipse中Attach Source
在Eclipse中,有时需要查看类.方法和变量的声明和定义的源代码. 但是在F3查看一些在JDK库中声明/定义的类.方法和 变量的源代码时,Eclipse给你打开的却是相应的.class文件(byte ...