fopen & fcolse & fseek & ftell & fstat 文件操作函数测试

1、文件大小查询file_size.c

  方法一:fseek + ftell;

  方法二:ftell

 1 #include <stdio.h>
 2 #include <fcntl.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <errno.h>
 6 
 7 void errHandling(const char *errMsg)
 8 {
 9     printf("%s: %s\n", errMsg, strerror(errno));
10     exit(-1);
11 }
12 
13 long getFileSize1(FILE *fp)
14 {
15     if (fseek(fp, 0, SEEK_END) != 0)
16     {
17         errHandling("fseek() fail");
18     }    
19     return ftell(fp);
20 }
21 
22 long getFileSize2(int fd)
23 {
24     struct stat st;
25     if ((fstat(fd, &st)) != 0)
26     {
27         errHandling("fstat() fail");
28     }
29     return st.st_size;
30 }
31 
32 int main(int argc, char *argv[])
33 {
34     if (argc != 2)
35     {
36         printf("Usage: %s <file_name>\n", argv[0]);
37         exit(-1);        
38     }
39     
40     FILE *fp = fopen(argv[1], "r");
41     if (NULL == fp)
42     {
43         errHandling("open() fail");
44     }
45     
46     printf("The size of %s: %ld bytes (fseek+ftell)\n", argv[1], getFileSize1(fp));
47     printf("The size of %s: %ld bytes (fstat)\n", argv[1], getFileSize2(fileno(fp)));
48     
49     fclose(fp);
50     exit(0);
51 }

2、特定大小文件创建以及读取操作时间测试 read_file_time.c

  描述:创建1G大小文件,并完成顺序、逆序以及随机读取操作

  1 #include <stdio.h>
  2 #include <fcntl.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <errno.h>
  6 #include <sys/time.h>
  7 
  8 #define BUF_SIZE (1024 * 1024)
  9 #define READ_SIZE (BUF_SIZE * 1024)
 10 #define NUM_ROUND 1024
 11 
 12 void errHandling(const char *errMsg)
 13 {
 14     printf("%s: %s\n", errMsg, strerror(errno));
 15     exit(-1);
 16 }
 17 
 18 long getFileSize(int fd)
 19 {
 20     struct stat st;
 21     if ((fstat(fd, &st)) != 0)
 22     {
 23         errHandling("fstat() fail");
 24     }
 25     return st.st_size;
 26 }
 27 
 28 /* in sequence */
 29 unsigned long getReadTimeSeq(char *pbuf, FILE *pf)
 30 {
 31     int readCnt = 0;
 32     struct timeval bgn;
 33     struct timeval end;
 34     unsigned long timeCnt = 0;
 35     
 36     memset(&bgn, 0, sizeof(struct timeval));
 37     memset(&end, 0, sizeof(struct timeval));
 38     printf("Start read in sequence\n");
 39     gettimeofday(&bgn, NULL);
 40     while (readCnt < READ_SIZE)
 41     {
 42         memset(pbuf, 0, BUF_SIZE);
 43         readCnt += fread(pbuf, 1, BUF_SIZE, pf);
 44         //printf("read %d MB\n", readCnt >> 20);
 45     }
 46     gettimeofday(&end, NULL);
 47     return ((end.tv_sec - bgn.tv_sec) * 1000000 + (end.tv_usec - bgn.tv_usec));
 48 }
 49 
 50 /* inverted sequence */
 51 unsigned long getReadTimeInvertSeq(char *pbuf, FILE *pf)
 52 {
 53     int readCnt = 0;
 54     long shift = READ_SIZE - BUF_SIZE;
 55     struct timeval bgn;
 56     struct timeval end;
 57     unsigned long timeCnt = 0;
 58 
 59     memset(&bgn, 0, sizeof(struct timeval));
 60     memset(&end, 0, sizeof(struct timeval));
 61     //printf("Start read in inverted sequence\n");
 62     gettimeofday(&bgn, NULL);
 63     while (readCnt < READ_SIZE)
 64     {
 65         fseek(pf, shift, SEEK_SET);
 66         memset(pbuf, 0, BUF_SIZE);
 67         readCnt += fread(pbuf, 1, BUF_SIZE, pf);
 68         shift -= readCnt;
 69         //printf("read %d MB\n", readCnt >> 20);
 70     }
 71     gettimeofday(&end, NULL);
 72     return ((end.tv_sec - bgn.tv_sec) * 1000000 + (end.tv_usec - bgn.tv_usec));
 73 }
 74 
 75 /* Random sequence */
 76 unsigned long getReadTimeRandPos(char *pbuf, FILE *pf)
 77 {
 78     int readCnt = 0;
 79     long shift = READ_SIZE - BUF_SIZE;
 80     struct timeval bgn;
 81     struct timeval end;
 82     unsigned long timeCnt = 0;
 83 
 84     memset(&bgn, 0, sizeof(struct timeval));
 85     memset(&end, 0, sizeof(struct timeval));
 86     srand((int)time(0));
 87     //int num = 0;
 88     gettimeofday(&bgn, NULL);
 89     while (readCnt < READ_SIZE)
 90     {
 91         //++num;
 92         shift = BUF_SIZE * (rand() % NUM_ROUND);
 93         fseek(pf, shift, SEEK_SET);
 94         memset(pbuf, 0, BUF_SIZE);
 95         readCnt += fread(pbuf, 1, BUF_SIZE, pf);
 96     }
 97     gettimeofday(&end, NULL);
 98     //printf("num = %d\n", num);
 99     return ((end.tv_sec - bgn.tv_sec) * 1000000 + (end.tv_usec - bgn.tv_usec));
100 }
101 
102 int main(int argc, char *argv[])
103 {
104     if (argc != 2)
105     {
106         printf("Usage: %s <file_name>\n", argv[0]);
107         exit(-1);        
108     }
109     
110     FILE *pf = fopen(argv[1], "w+");
111     if (NULL == pf)
112     {
113         errHandling("open() fail");
114     }
115     /*生成大小为1G的文件*/
116     fseek(pf, READ_SIZE, SEEK_SET);
117     fputc(166, pf);
118     rewind(pf);
119 
120     char *buf = (char *)malloc(BUF_SIZE * sizeof(char));
121     if (NULL == buf)
122     {
123         errHandling("malloc() fail");
124     }
125 
126     printf("Time in sequence: timeCnt = %ld us\n", getReadTimeSeq(buf, pf));    
127     printf("Time in inverted sequence: timeCnt = %ld us\n", getReadTimeInvertSeq(buf, pf));
128     printf("Time in random sequence: timeCnt = %ld us\n", getReadTimeRandPos(buf, pf));
129     
130     fclose(pf);
131     free(buf);
132     exit(0);
133 }

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)

  统一编译以上两个源文件,并生成两个对应的可执行文件

猜你喜欢

转载自www.cnblogs.com/Glory-D/p/9502283.html