将单词(char [])插入C中的结构字(char [])

时间:2015-01-08 17:57:55

标签: c struct text-files scanf

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

的结构变量中

我的主要问题标注在评论下#34;我的问题在下面"

结构代码:

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

3 个答案:

答案 0 :(得分:2)

删除&

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

答案 1 :(得分:0)

这一行:

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

答案 2 :(得分:0)

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

比较应该是: