このプログラムを実行すると,次のようになります。
コンソール画面でabcdefgと打ってエンターキーを押すと次が出力される。
<<a>>
<<c>>
<<e>>
<<abcdefg>>と表示されません。なぜでしょうか?実行環境はVisual Studio2017です。
Main.cpp
C
1#include <stdio.h> 2#include "get_word.h" 3 4int main(void) 5{ 6 char buf[1024]; 7 while (get_word(buf, 1024, stdin) != EOF) { 8 printf("<<%s>>\n", buf); 9 } 10 return 0; 11} 12 13
###get_word.h
c
1#ifndef GET_WORD_H_INCLUDED 2#define GET_WORD_H_INCLUDED 3#include <stdio.h> 4 5int get_word(char *buf, int size, FILE *stream); 6 7#endif // !GET_WORD_H_INCLUDED 8
###get_word.cpp
c
1#include <stdio.h> 2#include <ctype.h> 3#include <stdlib.h> 4 5int get_word(char *buf, int buf_size, FILE *fp) 6{ 7 int len; 8 int ch; 9 10 while ((ch = getc(fp)) != EOF && !isalnum(ch)); 11 12 if (ch == EOF) { 13 printf("EOF\n"); 14 return EOF; 15 } 16 17 len = 0; 18 do { 19 buf[len] = ch; 20 len++; 21 22 if (len >= buf_size) { 23 fprintf(stderr, "were too long.\n"); 24 exit(1); 25 } 26 } while ((ch = getc(fp)) != EOF && !isalnum(ch)); 27 28 buf[len] = '\0'; 29 30 return len; 31 32}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。