473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

18 New Member
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
4 1962
Savage
1,764 Recognized Expert Top Contributor

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
Avon
18 New Member
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
1,764 Recognized Expert Top Contributor
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
1,764 Recognized Expert Top Contributor
Thanks a lot for finding the problem. :)
I'm more than happy!

Savage
May 27 '07 #5

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

Similar topics

25
by: Maurice LING | last post by:
Hi, I think I've hit a system limit in python when I try to construct a list of 200,000 elements. My error is malloc: vm_allocate (size = 2400256) failed...... Just wondering is this...
6
by: Ganesan selvaraj | last post by:
I using C# .net. i want to split the text files based of the some condition. my source text file size may be 4 kb to 4 gb. some time when i split the i got the "out of memory exception. when i...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
3
by: sam_cit | last post by:
Hi, Everybody, i have a situation where i receive data in a buffer and i read the buffer and store it in a linked list, one the problem is i can receive any number of data... so i might run out...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
13
by: kolmogolov | last post by:
/* Hi, I have removed things irrelevant to reproducing the problem. What's wrong with my code? Thanks in advance for any hint! 1166425362 */ #include <stdio.h> #include <stdlib.h> #include...
3
by: Kane | last post by:
When you create node 1 you allocate memory and link it Again when you create node 2 you would allocate memory for that node in a different section of the code. Is there more efficient way where I...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.