Could you please point me to what am I doing wrong?
I am trying to write some code that will read data from a text file and save those data into an array of pointers, that point to to structs. It is critical that I don't use any global identifiers.
This is what I wrote but every time the function nactiProdukty (readProductsfromFile) ends it crasches with an error: First-chance exception at 0x73006500 in ConsoleApplication3.exe: 0xC0000005: Access violation executing location 0x73006500. But the reading from file seems to be working ok.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct produkt {
char jmeno[20];
int mnozstvi;
int cena;
} tProdukt;
int SpoctiProdukty();
int Generuj(int min, int max);
void nactiProdukty(tProdukt **pole);
void main(){
tProdukt **pole=NULL;
int i;
srand(time(NULL));
nactiProdukty(pole);
printf("test");
scanf("%s");
}
int SpoctiProdukty(){
FILE *data=fopen("data.txt","r");
int count=0;
while(fscanf(data,"%s %d") != EOF){
count++;
}
fclose(data);
return count;
}
int Generuj(int min, int max){
return (rand()%(max-min)+min);
}
void nactiProdukty(tProdukt **pole){
FILE *data=fopen("data.txt","r");
int temp;
int i;
char temps[20];
int pocet=SpoctiProdukty();
//tProdukt **pole;
pole=(tProdukt**)malloc(sizeof(tProdukt*)*pocet);
for (i = 0; i < pocet; i++) {
pole[i]=(tProdukt*)malloc(sizeof(tProdukt));
}
for (i = 0; i < pocet; i++) {
fscanf(data,"%s %d",temps,&temp);
strcpy((*pole[i]).jmeno,temps);
(*pole[i]).cena=temp;
(*pole[i]).mnozstvi=Generuj(10,150);
}
}