[英]Reading word by word from text file in C
I am very new to C. I am trying to read the words from a file which contains lots of not alpha characters. 我是C的新手。我正在尝试从包含许多非字母字符的文件中读取单词。 My input file looks something like this
%tOm12%64ToMmy%^$$6
and I want to read tom first and then put tom in my data structure and then read tommy and put that in my data structure all in lowercase. 我的输入文件看起来像这样
%tOm12%64ToMmy%^$$6
我想首先读取tom然后将tom放入我的数据结构然后读取tommy并将其放在我的数据结构中全部用小写字母表示。 This is what I have tried until now. 这是我迄今为止所尝试过的。 All my other code works as I have manually sent the parameters to the methods and there are no errors.
我的所有其他代码都工作,因为我手动将参数发送到方法,没有错误。 This is what I have tried to read the words from the file.
这是我试图从文件中读取的文字。 A word can be of 100 characters max.
一个单词最多可以包含100个字符。 Can someone help me understand the logic and possibly this code.I am very lost.Thank You!
有人可以帮助我理解逻辑和可能的代码。我很失落。谢谢!
void read(FILE *fp)
{
FILE *fp1 = fp;
char word[100];
int x;
int counter = 0;
while ((x = fgetc(fp1)) != EOF)
{
if (isalpha(x) == 0)
{
insert(&tree,word);
counter = 0;
}
if (isalpha(x) != 0)
{
tolower(x);
word[counter] = x;
counter++;
}
}
rewind(fp1);
fclose(fp1);
}
char *getWord(FILE *fp){
char word[100];
int ch, i=0;
while(EOF!=(ch=fgetc(fp)) && !isalpha(ch))
;//skip
if(ch == EOF)
return NULL;
do{
word[i++] = tolower(ch);
}while(EOF!=(ch=fgetc(fp)) && isalpha(ch));
word[i]='\0';
return strdup(word);
}
void read(FILE *fp){
char *word;
while(word=getWord(fp)){
insert(&tree, word);
}
//rewind(fp1);
fclose(fp);
}
This is a simplification of @BLUEPIXY 's answer. 这是@BLUEPIXY答案的简化。 It also checks the array boundaries for word[]
它还检查word []的数组边界
char *getword(FILE *fp)
{
char word[100];
int ch;
size_t idx ;
for (idx=0; idx < sizeof word -1; ) {
ch = fgetc(fp);
if (ch == EOF) break;
if (!isalpha(ch)) {
if (!idx) continue; // Nothing read yet; skip this character
else break; // we are beyond the current word
}
word[idx++] = tolower(ch);
}
if (!idx) return NULL; // No characters were successfully read
word[idx] = '\0';
return strdup(word);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.