dimanche 19 avril 2015

Dynamically Allocated Array of Pointers Keeps Rewriting Itself

I'm trying to write a program to find the frequency of words in a file (words.txt) using a dynamically allocated array of pointers to store the words and frequencies at which the words appear and print the results to another file (frequencies.txt).


Example:


Read from words.txt



apple
orange
apple
banana
orange
apple


Write to frequencies.txt:



3 apple
2 orange
1 banana


Here's the program



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


For now I'm simply trying to get the first value assigned (1 apple). The problem that I'm having is that when I try to assign the first value of the dynamic array, the value for wordarray->word changes with each read from the file, but it's supposed to stay as apple:



Word: apple
1 apple

Word: orange
1 orange

Word: apple
1 apple

Word: banana
1 banana

Word: orange
1 orange

Word: apple
1 apple

Results:
1 apple


Any advice is appreciated.


Aucun commentaire:

Enregistrer un commentaire