我想从文件中提取单词,然后发送到数组。我是用fscanf完成的。但是,它需要数字,字符(,。%&#!?)和其他东西。如何控制此语句?
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);
}
最佳答案
使用"%[]"
区分字母和非字母。
#define NWORDS (1000)
char kelime[NWORDS][50];
size_t i;
for (i=0; i<NWORDS; i++) {
// Toss ("*" indicates do not save) characters that are not ("^" indicates `not`) letters.
// Ignore return value
fscanf(dosya, "%*[^A-Za-z]");
// Read letters
// Expect only a return value of 1:Success or EOF:no more file to read
// Be sure to limit number of characters read: 49
if (fscanf(dosya, "%49[A-Za-z]", kelime[i]) != 1) break;
}
// do something with the `i` words
关于c - C-如何从不带数字,特殊字符(,。?&)的文件中提取单词?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30242172/