0

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);
  }
}
6
  • This is C++ not C#, post with the C++ tag. Commented Jan 11, 2013 at 12:58
  • And don't include tags in your title. That's what the tags field is for. =) Commented Jan 11, 2013 at 12:59
  • 1
    @Alex What gave you the impression that this is C++? Though I agree that it isn't C# either. Commented Jan 11, 2013 at 13:01
  • -1 The formatting on your code leaves much to be desired. Commented Jan 11, 2013 at 13:25
  • 2
    I have repaired it, please observe the new formatting and emulate it in the future: sensible indentations, uniform indentations, all blocks indented, no needlessly large gaps in the code Commented Jan 11, 2013 at 13:31

1 Answer 1

1

The line

while(fscanf(data,"%s %d") != EOF){

is wrong. From the fscanf man page:

If the number of conversion specifications in format exceeds the number of pointer arguments, the results are undefined.

Crashing is a valid and common undefined result. You can fix this by providing variables for fscanf to write to, then ignoring the results:

int i;
char s[20];
while(fscanf(data,"%s %d", s, i) == 2){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.