Dunque alla fine sono riuscita a creare qualcosa di funzionante.
Non so se è il modo migliore per risolvere il problema ma vorrei postare lo stesso il mio codice, potrebbe essere utile a qualcuno o chissà anche una buona occasione per confrontarsi.
codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define ROW 100
#define COL 20+2 /* 20 + \n + \0 */
int load(char words[ROW][COL], int*occurences); /* definizione funzione per caricare le occorrenze */
int find(char words[ROW][COL], int*occurences, int numeroparole);// definizione funzione per trovare le occorrenze nel 2file
// main con chiamata alle funzioni
int main (void)
{
FILE *fpw;
char words[ROW][COL];
int numeroparole, *occurences, i;
numeroparole=load(words, occurences);
fpw = fopen("output.txt", "w");
if (fpw == NULL) {
printf("Error opening the output text file!\n");
return 0;
}
printf("numeroparole da cercare = %d\n parole:\n\t", numeroparole);
for (i=0; i<numeroparole; i++)
printf("%s\n\t", words[i]);
printf("-----------------------\n");
if (numeroparole!=0) {
occurences = (int*)malloc(numeroparole*sizeof(int));
find(words, occurences, numeroparole);
}
for (i=0; i<numeroparole; i++){
fprintf(fpw," occorrenze di %s: ", words[i]);
fprintf(fpw,"%d\n", occurences[i]);
}
system("PAUSE");
return 0;
}
//funzione che legge dal primo file e carica le occorrenze da trovare in un vettore "words"
int load(char words[ROW][COL], int*occurence)
{
FILE *fp;
int n, i, l;
char parola[20];
fp=fopen("occorrenze.txt", "r");
if (fp == NULL) {
printf("Error opening the occorrenze file!\n");
return 0;
}
fscanf(fp, "%d", &n);
for (i=0; i<n; i++){
fscanf (fp, "%s", parola);
for (l=0; l<strlen(parola); l++)
parola[l]=tolower(parola[l]);
strcpy(words[i], parola);
}
fclose(fp);
return n;
}
//funzione che confronta le occorrenze con il 2 file
int find(char words[ROW][COL], int*occurences, int numeroparole)
{
FILE *fpr, *fpw;
char riga[100];
int id, k, l;
for (k=0; k<numeroparole; k++)
occurences[k]=0;
fpr = fopen("citazioni.txt", "r");
if (fpr == NULL) {
printf("Error opening the input text file!\n");
return 0;
}
fpw = fopen("output.txt", "w");
if (fpw == NULL) {
printf("Error opening the output text file!\n");
return 0;
}
while (fscanf(fpr, "%s", riga) != EOF) {
for (l=0; l<strlen(riga); l++)
riga[l]=tolower(riga[l]);
for (k=0; k<numeroparole; k++){
if (strcmp(words[k], riga)==0){
occurences[k]++;
}
}
}
fclose(fpr);
fclose(fpw);
return 1;
}