【发布时间】:2015-05-14 16:14:50
【问题描述】:
我想从文件中取词。我被发送到数组。我用 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);
}
【问题讨论】:
-
不要使用
while (!feof(...)),它在大多数情况下不会按预期工作。而是使用例如while (fscanf(dosya, "%s", &kelime[i]) == 1). -
对于您的问题,您可能会发现this
scanf(and family) reference 很有用,尤其是关于"%["格式的部分。 -
顺便说一句
fclose应该在if ... != NULL检查内。关闭NULL时,某些(大多数?)系统会崩溃。 -
Not Found File !是所谓的无用错误消息。if((dosya = fopen(path,"r")) == NULL ) { perror(path);}