By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,376 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,376 IT Pros & Developers. It's quick & easy.

Memory error when I try to get list data from a text file in C

P: 18
Hello there.

I have a problem with text files in C. Basically what I do is, let the user save the data from a singly-linked list, in to a .txt using this code:
Expand|Select|Wrap|Line Numbers
  1. while (list!=NULL)
  2.     {
  3.     fprintf(fp,"%s %s %s %c %f %d\n",list->kodikos,list->perigrafi,list->etaireia,list->diathesimotita,list->timi,list->posotita);
  4.       list=list->next;
  5.     }
Then in order to be sure, I check the .txt file so as to see that all the data is stored there. And indeed it is.

However when I try to load the list on my program, I get a memory (?) error. Here's the code I use:

Expand|Select|Wrap|Line Numbers
  1.     while (!feof(fp))
  2.         {
  3.           fscanf(fp,"%s %s %s %c %f %d\n",buffer_kod,buffer_per,buffer_et,&temp->diathesimotita,&temp->timi,&temp->posotita);
  4.           temp->kodikos=strdup(buffer_kod);
  5.           temp->perigrafi=strdup(buffer_per);
  6.           temp->etaireia=strdup(buffer_et);
  7.           printf("This is the current string: %s\n", temp->kodikos);
  8.             proion=insert(proion,temp);
  9.           }
  10.     fclose(fp);
  11.     printf("File '%s', loaded.\n", s);
  12.     menu();
"proion" is the pointer to the full list, while "temp" is the pointer to new node.
Function "insert" is a typical function that gets new nodes and puts them sorted on the full list:

Expand|Select|Wrap|Line Numbers
  1. struct lista *insert (struct lista *p,struct lista *list)
  2. {
  3.     int cond;
  4.     struct lista *n;
  5.  
  6.     if (p==NULL)
  7.     {
  8.         p=(struct lista *)malloc(sizeof(struct lista));
  9.         p->kodikos=strdup(list->kodikos);
  10.         p->perigrafi=strdup(list->perigrafi);
  11.         p->etaireia=strdup(list->etaireia);
  12.         p->timi=list->timi;
  13.         p->diathesimotita=list->diathesimotita;
  14.         p->posotita=list->posotita;
  15.         p->next=NULL;
  16.         printf("Product not found. Added to the full list!\n\n");
  17.     }
  18.     else if ((cond = strcmp(list->kodikos,p->kodikos)) == 0)
  19.     {
  20.         p->posotita = p->posotita + list->posotita;
  21.           printf("Product found. Quantity updated!\n\n");
  22.     }
  23.     else if (cond < 0)
  24.     {
  25.          n=(struct lista *)malloc(sizeof(struct lista));
  26.         n->kodikos=strdup(list->kodikos);
  27.         n->perigrafi=strdup(list->perigrafi);
  28.         n->etaireia=strdup(list->etaireia);
  29.         n->timi=list->timi;
  30.         n->diathesimotita=list->diathesimotita;
  31.         n->posotita=list->posotita;
  32.         n->next=p;
  33.         p=n;
  34.         printf("Product not found. Added to the full list!\n\n");
  35.     }
  36.     else if (cond > 0)
  37.     {
  38.         p->next = insert(p->next, list);
  39.     }
  40.  
  41.     return p;
  42.  
  43.  
  44. }
The program manages to end the (!feof) loop succesfully, since printf on line 7 keeps producing the right results and the insert function gives me the correct messages of "product not found. Added to the list" etc. When the loop ends I get the printf message on line 11 that says "File loaded" and the menu comes back again. However when it does come up again I'm not allowed to type anything and I get a memory error. Anyone know why's that happening?

Thanks. ;)
May 27 '07 #1
Share this Question
Share on Google+
4 Replies


Savage
Expert 100+
P: 1,764

The program manages to end the (!feof) loop succesfully, since printf on line 7 keeps producing the right results and the insert function gives me the correct messages of "product not found. Added to the list" etc. When the loop ends I get the printf message on line 11 that says "File loaded" and the menu comes back again. However when it does come up again I'm not allowed to type anything and I get a memory error. Anyone know why's that happening?

Thanks. ;)
Have u allocated memory for temp?

Savage
May 27 '07 #2

P: 18
No... Followed your suggestion and it seems to be working, although... care to explain why? I mean if it was a memory problem for "temp" why does it allow me to enter values on the first place? :S

Thanks a lot for finding the problem. :)
May 27 '07 #3

Savage
Expert 100+
P: 1,764
No... Followed your suggestion and it seems to be working, although... care to explain why? I mean if it was a memory problem for "temp" why does it allow me to enter values on the first place? :S

Thanks a lot for finding the problem. :)
U allways need to allocate memory for all pointers to struct.

When memory is not allocated u can input data, but that data is garbage.

Compiler wont send any warning,it's analog with call to scanf without adress operator.

Savage
May 27 '07 #4

Savage
Expert 100+
P: 1,764
Thanks a lot for finding the problem. :)
I'm more than happy!

Savage
May 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.