我想從文件中取詞,並將其發送給數組。我用fscanf做了這個。但是,它正在考慮數字,字符(以及其他內容)。我怎樣才能控制這種說法?C - 我如何從沒有數字,特殊字符(,。?&)的文件中取詞?
int main(void)
{
char path[100];
printf("Please, enter path of file: "); scanf("%s",&path);
FILE *dosya;
char kelime[1000][50];
int i=0;
if((dosya = fopen(path,"r")) != NULL)
{
while(!feof (dosya))
{
fscanf(dosya,"%s",&kelime[i]);
i++;
}
}
else{printf("Not Found File !");}
fclose(dosya);
}
不要使用'while(!feof(...))',它在大多數情況下不會按預期工作。使用例如'while(fscanf(dosya,「%s」,&kelime [i])== 1)'。 –
至於你的問題,你可能會發現[這個'scanf'(和家庭)引用](http://en.cppreference.com/w/c/io/fscanf)有用,特別是關於''%[ 「'格式。 –
順便說一句'fclose'應該在'if ...!= NULL'檢查中。一些(大多數?)系統在關閉「NULL」時會崩潰。 – anatolyg