開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の22章(まとめのプログラミング)、22.11(プログラミング実習)、実習22-1.を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
実習22-1.
コード
sample.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *in_file;
in_file = fopen(*(++argv), "r");
void get_word(FILE *in_file);
if(in_file == NULL){
fprintf(stderr, "can't open file '%s'\n", *(++argv));
exit (8);
}
get_word(in_file);
fclose(in_file);
return (0);
}
void get_word(FILE *in_file)
{
char ch;
char word1[10];
char word2[10];
int i = 0;
while(1){
ch = fgetc(in_file);
if(ch == EOF){
return;
}
if(isalpha(ch)){
word2[i] = ch;
i++;
} else {
word2[i] = '\0';
break;
}
}
strcpy(word1, word2);
i = 0;
while(1){
ch = fgetc(in_file);
if(ch == EOF){
word2[i] = '\0';
if(strcmp(word1, "") != 0 && strcmp(word1, word2) == 0){
printf("%s\n", word1);
}
return;
}
if(isalpha(ch)){
word2[i] = ch;
i++;
} else {
word2[i] = '\0';
if(strcmp(word1, "") != 0 && strcmp(word1, word2) == 0){
printf("%s\n", word1);
}
strcpy(word1, word2);
i = 0;
}
}
}
入出力結果(Terminal)
$ cc -g -o sample sample.c $ ./sample temp.txt the in file $ cat temp.txt !! @@ ## $$ %% in the the file in in the file in the file file !! in the file $
0 コメント:
コメントを投稿