Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言逐字读取文本文件_C_Char_Fopen_Fgetc_File Read - Fatal编程技术网

用C语言逐字读取文本文件

用C语言逐字读取文本文件,c,char,fopen,fgetc,file-read,C,Char,Fopen,Fgetc,File Read,我是C语言的新手。我试图从一个包含大量非字母字符的文件中读取单词。我的输入文件看起来像这样%tOm12%64ToMmy%^$$6,我想先读tom,然后把tom放在我的数据结构中,然后再读tommy,然后把它放在我的数据结构中,都是小写的。这是我迄今为止一直在尝试的。我的所有其他代码都可以正常工作,因为我已经手动将参数发送到方法,并且没有错误。这就是我试图从文件中读出的单词。一个单词最多可以有100个字符。有人能帮我理解逻辑和可能的代码吗?我很迷茫。谢谢 void read(FILE *fp) {

我是C语言的新手。我试图从一个包含大量非字母字符的文件中读取单词。我的输入文件看起来像这样
%tOm12%64ToMmy%^$$6
,我想先读tom,然后把tom放在我的数据结构中,然后再读tommy,然后把它放在我的数据结构中,都是小写的。这是我迄今为止一直在尝试的。我的所有其他代码都可以正常工作,因为我已经手动将参数发送到方法,并且没有错误。这就是我试图从文件中读出的单词。一个单词最多可以有100个字符。有人能帮我理解逻辑和可能的代码吗?我很迷茫。谢谢

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);
}

这是@BLUEPIXY答案的简化。它还检查单词[]的数组边界

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);
}
char*getword(文件*fp)
{
字符字[100];
int-ch;
尺寸(idx);;
对于(idx=0;idx
首先,这里没有定义树变量,所以您应该解释它的作用。其次,字符数组应以“\0”结尾,因此将其设为“字符字[101]”,并以“字[++计数器]=”结尾循环。'\0'您缺少设置
word
的终止空字符的代码。添加
word[counter]='\0';
在重置
计数器之前
@cisforcoockies tree是一个全局变量,我这样做了,但我正在尝试使它读取第一个单词,即tom,然后将其插入树中,然后读取第二个单词并插入树中,但插入tom后该单词不清楚。那么我如何使它成为c学习数组,然后将一个新词插入到相同长度的字符数组中。@RSahu我这样做了,但程序编译得很好,但正确的词没有发送。我正在尝试使它读取tom,然后发送到树。然后字符数组被清除。它读取tommy发送到树,然后字符数组被清除。我添加了这个词[counter]='\0';但它不工作。
word[counter]=x;
对于
x
太大没有保护作用。它可以工作!但是你能解释一下你到底做了什么吗?我理解read方法,但对getWord方法有点迷茫。如果可能的话,你能解释一下吗?我是C编程新手,我想确保我理解它!@code4life 1)前面需要分隔符要跳过的单词2)当到达EOF时,必须在存储期间输出字符。3)终止NUL字符必须使用
word
。4)插入操作可能需要克隆字符串。
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);
}