#include <stdio.h>
#include <stdlib.h>
#include <time.h>
long countln(FILE *);
char buf[BUFSIZ*4]; /* File buffer */
int main()
{
long c;
time_t start_t, end_t;
FILE *stream;
/*
標準バッファ
*/
stream = fopen("test", "r");
time(&start_t);
c = countln( stream );
time(&end_t);
fclose(stream);
printf("バッファ使用(Size : %d) Time: %f\n",
BUFSIZ, difftime(end_t, start_t));
/*
大きいバッファ
*/
stream = fopen("test", "r");
setvbuf(stream, buf, _IOFBF, sizeof(buf));
time(&start_t);
c = countln( stream );
time(&end_t);
fclose(stream);
printf("バッファ使用(Size : %d) Time: %f\n",
BUFSIZ*4, difftime(end_t, start_t));
/*
バッファ未使用
*/
stream = fopen("test", "r");
setvbuf(stream, NULL, _IONBF, 0);
time(&start_t);
c = countln( stream );
time(&end_t);
fclose(stream);
printf("バッファ未使用(Size : 0) Time: %f\n", difftime(end_t, start_t));
return 0;
}
/****************************************/
/* テキストファイルの行数のカウント */
/* stream : ストリーム */
/* return : 行数 */
/****************************************/
long countln(FILE *stream)
{
long c = 0;
char linebuf[256];
while(!feof(stream)) {
if(fgets(linebuf, 255, stream) == NULL)
break;
++c;
}
return c;
}