/* 日本の空港コードを検索し、表示するプログラム */ #include #include #include int main() { FILE *fp; char code[4], airport[30]; /* 空港コードと空港名用文字配列 */ char buf[4]; /* キーボード入力用文字配列 */ int status; /* 適宜変数は追加して良い */ while(1) { printf("Input IATA code: "); if (scanf("%s",buf)==EOF) break; /* ファイルオープン */ if ((fp=fopen("IATA-jp.txt","r"))==NULL) { printf("Code file open error!!\n"); exit(2); } /* ここに、空港コードを読み込みながら、一致する ものがあるかをチェックする処理と、結果を表示する 処理を追加する */ while(1) { status=fscanf(fp, "%s%s",code, airport); if (status==EOF) break; if (strcmp(code, buf)==0) break; } if (status==EOF) printf("%s is not found.\n",buf); else printf("%s is %s Airport.\n",code, airport); fclose(fp); } printf("\n"); return 0; }