20131224【C言語】ifdefディレクティブを使いデバッグする
お題
ifdefディレクティブを使いデバッグする
プログラム概要
ファイルをコピーする。
DEBUGというマクロで定義されていればコピーしながら
コピー内容を標準出力させる
ソース
#include
#include
int main(int argc, char *argv[])
{
FILE *from, *to;
char ch;
/* Validate arguments */
if (argc != 3) {
printf("Usage: < Traget file>\n");
exit(1);
}
/* Open source file */
if *1 == NULL) {
printf("Cannot open source file.\n");
exit(1);
}
/* Open target file */
if *2 == NULL) {
printf("Cannot open target file.\n");
exit(1);
}
/* File copy */
while (!feof(from)) {
ch = fgetc(from);
if (ferror(from)) {
printf("Read error.\n");
exit(1);
}
if (!feof(from)) {
fputc(ch, to);
#ifdef DEBUG
putchar(ch);
#endif
}
if(ferror(to)) {
printf("Write error.\n");
exit(1);
}
}
fclose(from);
fclose(to);
return 0;
}
実行結果 ※コンパイル時にオプションDにDEBUGを指定して実行
# cat file1
Merry christmas!
# ./a.out file1 file2
Merry christmas!
# cat file2
Merry christmas!