開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料、light版)
- 言語: C
- コンパイラ: UNIX ccコンパイラ (汎用UNIX)
『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の14章(ファイルの入出力)14.2(変換ルーチン)設問14-1を解いてみる。
設問14-1.
fgetsは改行文字も含めて1行読み取るから。
修正
コード(TextWrangler)
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ char name[100]; FILE *in_file; printf("Name? "); fgets(name, sizeof(name), stdin); name[strlen(name) - 1] = '\0'; in_file = fopen(name, "r"); if(in_file == NULL){ fprintf(stderr, "Could not open file\n", name); exit(8); } printf("File found\n"); fclose(in_file); return (0); }
入出力結果(Terminal)
$ cc -g -o sample sample.c $ ./sample Name? sample.c File found $ ./sample Name? sample File found $ ./sample Name? abcde Could not open file $
0 コメント:
コメントを投稿