英単語を入力して意味を出力するプログラムを作成しているのですが、空白で区切られている場合の出力の仕方がわかりません。
ファイルをdic.txtとし、たとえば、
bear 熊 cat 猫 dog 犬 horse 馬 wolf 狼
の時にscanfした英単語がファイルにある場合右の意味を出力させたいのですが、
#include <stdio.h> #include <string.h> int main( void ) { FILE* fp; char wrd[ 256 ], dic[ 256 ], *p; printf( "英単語を入力 > " ); scanf( "%s", wrd ); if( NULL == ( fp = fopen( "dic.txt", "r" ) ) ) { printf( "file open error.\n" ); return 1; } for( ; ; ) { if( NULL == fgets( dic, sizeof( dic ), fp ) ) { printf( "not found.\n" ); break; } if( 0 != strncmp( wrd, dic, strlen( wrd ) ) ) { continue; } if( NULL == ( p = strchr( dic, '\n' ) ) ) { printf( "dictionary file error.\n" ); break; } printf( "%s", p + 1 ); break; } fclose( fp ); return 0; }
と作成したのですがうまく動作しません。
いまいちファイル操作がわからず困っています。
うまく動作しませんとありますが,どのように動作しないのでしょうか.