C 动态分配的指针数组不断重写自身

C 动态分配的指针数组不断重写自身,c,string,pointers,structure,C,String,Pointers,Structure,我正在尝试编写一个程序,使用一个动态分配的指针数组来存储单词和单词,以查找文件(words.txt)中单词的频率 单词出现的频率,并将结果打印到另一个文件(frequencies.txt) 例如: 从words.txt读取 apple orange apple banana orange apple 写入frequencies.txt: 3 apple 2 orange 1 banana 这是节目单 #include <stdio.h> #include <string.h

我正在尝试编写一个程序,使用一个动态分配的指针数组来存储单词和单词,以查找文件(words.txt)中单词的频率 单词出现的频率,并将结果打印到另一个文件(frequencies.txt)

例如:

从words.txt读取

apple
orange
apple
banana
orange
apple
写入frequencies.txt:

3 apple
2 orange
1 banana
这是节目单

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

struct wordfreq 
{
  int count;
  char *word;
};

typedef struct wordfreq wordfreq;

int main(int argc, char *argv[])
{
    wordfreq **wordarray;
    int size = 1, i, j, x, compare;
    char buffer[100];
    FILE *fp;

    if ( argc != 3 )
    {
        fprintf(stderr,"!!!ERROR!!!\nNUMBER OF MISSING PARAMETERS: %d\n", 3-argc);
        exit(-1);
    }

    fp = fopen(argv[1],"r");
    if ( fp == NULL )
    {
        perror(argv[1]);
        exit(-1);
    }

    wordarray = (wordfreq**)malloc(size*sizeof(wordfreq*));

    for(i = 0; i < size; i++)
    {
        wordarray[i] = (wordfreq *) malloc(sizeof(wordfreq*));
        wordarray[i]->word = "!";
        wordarray[i]->count = 0;
    }

    while(fscanf(fp,"%s",buffer) == 1)
    {
        printf("Word: %s\n", buffer);

        if(wordarray[0]->word == "!")
        {
            wordarray[0]->word = buffer;
            wordarray[0]->count = 1;
        }

        //Continued coding

        for(x = 0; x < size; x++)
        {
            printf("%d %s\n", wordarray[x]->count, wordarray[x]->word);
        }
        printf("\n");
    }

    //wordarray = realloc(wordarray,size*sizeof(wordfreq*));

    fclose(fp);

    fp = fopen(argv[2], "w");
    if ( fp == NULL )
    {
        perror(argv[1]);
        exit(-1);
    }

    for(i = 0; i < size; i++)
    {
        fprintf(fp, "%d %s\n", wordarray[i]->count, wordarray[i]->word);
    }

    fclose(fp);

    free(wordarray);

    return 0;
}

感谢您的建议。

wordarray[0]->word=buffer
替换为
wordarray[0]->word=strdup(buffer)

当前
wordarray[0]->word
是指向
buffer
的指针,因此每次运行行
fscanf(fp,“%s”,buffer)
时都会更改它的值

**不要忘记在最后释放strdup内存

for(i = 0; i < size; i++)
{
    free(wordarray[i]->word);
}
for(i=0;iword);
}

此外,您不知道
大小是多少。首先,您可以扫描文件并找出大小:

int size = 0;
while (!feof(fp))
{
    fscanf(fp, "%s\n", buffer);
    size++; //you have this many lines in the files
}
rewind(fp);

wordarray[0]->word=buffer
-这不是你想象的那样。所有这些操作都是将
缓冲区
的基址保存在
成员中。只有一个
缓冲区,完成后每个人都指向它。我想你需要一些动态分配和
strcpy
strdup
,如果你不介意走出标准库一点的话。你做得很好,strdup做到了。谢谢你,伙计。不会免费的(wordarry)这样做?不,它只会释放您为
wordarray
分配的空间,而不是为每个单词分配的空间
strdup
好的,谢谢。我是否需要为
wordarray->count
也这样做,因为我没有使用
strdup
?没有必要,你应该只在动态分配的变量上使用
free
。你对
大小的看法是正确的,但我还没有为数组处理多个空格。我只是想解决将值放入数组的问题,我计划使用
realloc()
根据需要增加数组的大小(这是我的教授想要的)。@WoernerBro,是的,我的建议有点欺骗,它需要读取文件两次,读取文件是一个缓慢的操作。你必须重新定位或链接列表之类的东西。
int size = 0;
while (!feof(fp))
{
    fscanf(fp, "%s\n", buffer);
    size++; //you have this many lines in the files
}
rewind(fp);