[英]C - How can I take words from file without numbers, special characters(, . ? &)?
我想從文件中提取單詞,然后發送到數組。 我是用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
您可以使用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()
,無法解析該語句。 所以你必須
按字符檢查它
如果不是特殊字符或數字,則添加到字符串
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.