我正在尝试逐字阅读,以下是我采用的逻辑。这可以很好地读取单词,除非它到达一行中的最后一个单词,它存储当前文件的最后一个单词和下一个新行的第一个单词。有人可以告诉我如何让它工作吗?
int c;
int i =0;
char line[1000]
do{
c = fgetc(fp);
if( c != ' '){
printf("%c", c);
line[i++] = c;
}else if((c == '\n')){
//this is where It should do nothing
}else{
line[i] = '\0';
printf("\\0 reached\n");//meaning end of one word has been reached
strcpy(wordArr[counter++].word, line);//copy that word that's in line[xxx] to the struct's .word Char array
i=0;//reset the line's counter
}//if loop end
} while(c != EOF);//do-while end
fp
是一个文件指针。
HI BABY TYPE MAYBE
TODAY HELLO CAR
HELLO ZEBRA LION DON
TYPE BABY
我得到(不带引号)
"HI"
"BABY"
"TYPE"
"MAYBE
TODAY"