20131017【C言語】ファイルの中身を1byteおきに表示する
お題
ファイルの中身を1byteおきに表示する
ソース
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
long l;
if (argc != 2) {
fprintf(stderr, "Usage: <Program Name> <File> <Name>.\n");
exit(1);
}
if *1 == NULL) {
fprintf(stderr, "Cannot open file.\n");
exit(1);
}
fseek(fp, 0, SEEK_END); /* Read End of file */
l = ftell(fp);
/* Back to file head */
fseek(fp, 0, SEEK_SET);
for(; l >= 0; l = l - 2L) {
putchar(fgetc(fp));
fseek(fp, 1L, SEEK_CUR);
}
fclose(fp);
return 0;
}
実行結果
# cat in.txt
abcd
# ./a.out in.txt
ac
*1:fp = fopen(argv[1], "rb"