開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の14章(ファイル入出力)、14.1(ファイル関数)、14.2(変換ルーチン)、設問 14-1を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
設問14-1.
コード
sample.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[100];
FILE *in_file;
printf("Name? ");
fgets(name, sizeof(name), stdin);
/* 改行文字をヌル「\0」(文字の終端)に変更 */
name[strlen(name) - 1] = '\0';
in_file = fopen(name, "r");
if (in_file == NULL){
fprintf(stderr, "Could not open file\n");
exit (8);
}
printf("File found\n");
fclose(in_file);
return (0);
}
makefile
CC=cc CFLAGS=-g sample: sample.c $(CC) $(CFLAGS) -o sample sample.c clean: rm -f sample
入出力結果(Terminal)
$ ./sample Name? sample.c File found $ ./sample Name? abcde Could not open file $
0 コメント:
コメントを投稿