fopen&fcolse&fseek&ftell&fstatファイル操作関数テスト

26667 ワード

1、ファイルサイズクエリーfile_size.c
方法1:fseek+ftell;
方法2:ftell
 1 #include 
 2 #include 
 3 #include 
 4 #include <string.h>
 5 #include 
 6 
 7 void errHandling(const char *errMsg)
 8 {
 9     printf("%s: %s
", 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
", 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)
", argv[1], getFileSize1(fp)); 47 printf("The size of %s: %ld bytes (fstat)
", argv[1], getFileSize2(fileno(fp))); 48 49 fclose(fp); 50 exit(0); 51 }

 
 
2、特定サイズファイルの作成及び読み込み操作時間テストread_file_time.c
説明:1 Gサイズのファイルを作成し、順序、逆順序、ランダム読み出しを完了します.
  1 #include 
  2 #include 
  3 #include 
  4 #include <string.h>
  5 #include 
  6 #include 
  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
", 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
"); 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
", 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
");
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
", 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
", 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
", 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
", getReadTimeSeq(buf, pf)); 127 printf("Time in inverted sequence: timeCnt = %ld us
", getReadTimeInvertSeq(buf, pf)); 128 printf("Time in random sequence: timeCnt = %ld us
", 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)

 
以上の2つのソースファイルを統一的にコンパイルし、対応する2つの実行可能ファイルを生成します.