I desperately need some help with an exercise my professor gave me. Essentially, I'm making a program using a structure and dynamic memory where it'll read a file where each line has one word, and it'll print to a new file each unique word and how many times it was in the file.
So for instance, if the file going in had this
apple
orange
orange
The file it prints to would say
apple 1
orange 2
So far, this is my code
#include <stdio.h>
#include <stdlib.h>
struct wordfreq {
int count;
char *word;
};
int main(int argc, char *argv[]){
int i;
char *temp;
FILE *from, *to;
from = fopen("argc[1]","r");
to = fopen("argv[1]","w");
struct wordfreq w1[1000];
struct wordfreq *w1ptr[1000];
for(i = 0; i < 1000; i++)
w1ptr[i] = NULL;
for(i = 0; i < 1000; i++)
w1ptr[i] = (struct wordfreq*)malloc(sizeof(struct wordfreq));
while(fscanf(from,"%256s",temp)>0){
}
for(i = 999; i >= 0; i--)
free(w1ptr[i]);
}
w1ptr should store a word from the file in the wordfreq file, then increment count in that array. I have no clue how to go about storing the word in *word though. Any help would be greatly appreciated