我想从文件中取出单词。我发送给数组。我用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);
}
答案 0 :(得分:2)
使用"%[]"
区分字母和非字母。
#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
答案 1 :(得分:-1)
您可以使用fgetc()
char str[1000];
while((ch=fgetc(fname))!=EOF)
{
// Write your code here;
if((ch>='0' && ch<='9') || (ch<'a' && ch>'z') || (ch<'A' && ch>'Z'))
continue;
str[i++]=ch;
}
str[i]='\0';
使用fscanf()
时,无法解析语句。所以你必须
通过char
如果不是特殊字符或数字,则添加到字符串