【问题标题】:Inserting a word(char[]) into a structure word(char[]) in C在C中将单词(char [])插入结构单词(char [])
【发布时间】:2015-01-08 17:57:54
【问题描述】:

所以我正在从包含许多不唯一单词的文本文件中读取每个单词。我应该找到唯一单词的数量并将这些单词存储到一个名为 word 的结构变量中

我的主要问题标记在“我的问题在下面”的评论下

结构代码:

  typedef struct {
  char word[101];
  int  freq;

  } WordArray;

代码:

  //temp[i].word is a temporary structure that scans ALL words
  //input[i].word is the structure with unique words

  word = fscanf(finput, "%s", &temp[i].word);

 //if the word in temp[i] is in input[j].word then it is not unique
 // so frequency of word is incremented

   for(j = 0; j < 200; j++) { 
     if(strcmp(temp[i].word,input[j].word) == 0){
        contains = 1;
        input[j].freq++;
     }
  }

     // MY PROBLEM IS HERE BELOW
    if(contains != 1) { // since contains is not 1 then it is unique

     input[i].word = temp[i].word // i want to put this word in input[i].word but this
                                  // produces incompatible type error
                                  // i tried strcpy and strncpy no luck...
     input[i].freq++;
     uniqueWords++;
  }

【问题讨论】:

    标签: c struct text-files scanf


    【解决方案1】:

    删除&amp;

    fscanf(finput, "%s", &temp[i].word);
    

    改成

    fscanf(finput, "%s", temp[i].word);
    

    或者取第一个元素的地址,相当于上一行

    fscanf(finput, "%s", &temp[i].word[0]);
    

    传递的数组衰减为指向数组第一个元素的指针,所以它们都是等价的

    fscanf(finput, "%s", temp[i].word); /* passed a pointer to the first elemn */
    fscanf(finput, "%s", &temp[i].word[0]); /* passed the address of the first element */
    

    另外,为fscanf 添加一个限制以防止像这样的缓冲区溢出

    fscanf(finput, "%100s", temp[i].word); /* passed a pointer to the first elemn */
    

    其中的数字应小于数组大小1

    这一行你不能分配给数组

    input[i].word = temp[i].word
    

    错了,你应该用strcpy

    strcpy(input[i].word, temp[i].word);
    

    【讨论】:

    • 当我用你的 fscanf 替换时,我仍然收到错误“错误:从类型‘char *’分配给类型‘char[101]’时不兼容的类型”
    • 你在哪一行得到了那个错误,你有temp[i].word = something;吗?
    • @geforce 我明白了,我在答案中添加了解决方案,评论中的意思是将temp[i].word 的第一个字符分配给input[i].word 的第一个字符,这就是它起作用的原因,但你不能分配给 c 中的数组。
    • @gefoce 它可以工作,但不要这样做,有一个标准功能可以使用strcpy
    • 你必须包含string.h
    【解决方案2】:

    这一行:

    word = fscanf(finput, "%s", &temp[i].word);
    

    应该更像:

    if( 1 != fscanf(finput, " %100s", temp[i].word) )
    { // fscanf failed
        perror( "fscanf failed");
        exit( EXIT_FAILURE );
    }
    
    // implied else, fscanf successful
    

    【讨论】:

    • 为什么要发布两个答案?选择一个,添加相关内容,然后您可以删除另一个。
    【解决方案3】:
    typedef struct {
        char word[101];
        int  freq;
    } WordArray;
    

    这只是模糊了代码,使其更难阅读/理解/维护,更好用:

    #define MAX_WORDS (200)
    #define MAX_WORD_LEN (100)
    
    struct WordArray 
    {
        char word[MAX_WORD_LEN+1];
        int  freq;
    };
    
    struct WordArray input[MAX_WORDS];
    

    对于阅读单词,计算重复:

        int  numWords = 0;
        BOOL dupFound = false;
        char tempWord[101];
    
        // prep save area 'input[]'
        memset( input, 0x00, sizeof(input) );
    
        // prep for first iteration through while loop
        memset(tempWord, 0x00, sizeof(tempWord) );
    
        // note leading ' ' in format string
        // note sizing of input to avoid buffer overrun
        while( 1 != fscanf(finput, " %[MAX_WORD_LEN]s", tempWord) ) 
        {
    
            dupFound = false;
            for j = 0;j<MAX_WORDS;j++)
            {
                if( 0 == strcmp(tempWord, input[j].word) )
                { // then dup word found
    
                    input[j].freq++;
                    dupFound = true;
                    break;
                } // end if
            } // end for
    
    
            if( !dupFound )
            { // then, add new word
    
                strcpy( input[numWords].word, tempWord );
                input[numWords].freq = 1;
                numWords++;
            } // end if
    
            // prep for next iteration of while loop
            memset(tempWord, 0x00, sizeof(tempWord) );
        } // end while
    

    比较应该是:

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 2016-05-25
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多