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: -
while (list!=NULL)
-
{
-
fprintf(fp,"%s %s %s %c %f %d\n",list->kodikos,list->perigrafi,list->etaireia,list->diathesimotita,list->timi,list->posotita);
-
list=list->next;
-
}
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: - while (!feof(fp))
-
{
-
fscanf(fp,"%s %s %s %c %f %d\n",buffer_kod,buffer_per,buffer_et,&temp->diathesimotita,&temp->timi,&temp->posotita);
-
temp->kodikos=strdup(buffer_kod);
-
temp->perigrafi=strdup(buffer_per);
-
temp->etaireia=strdup(buffer_et);
-
printf("This is the current string: %s\n", temp->kodikos);
-
proion=insert(proion,temp);
-
}
-
fclose(fp);
-
printf("File '%s', loaded.\n", s);
-
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: - struct lista *insert (struct lista *p,struct lista *list)
-
{
-
int cond;
-
struct lista *n;
-
-
if (p==NULL)
-
{
-
p=(struct lista *)malloc(sizeof(struct lista));
-
p->kodikos=strdup(list->kodikos);
-
p->perigrafi=strdup(list->perigrafi);
-
p->etaireia=strdup(list->etaireia);
-
p->timi=list->timi;
-
p->diathesimotita=list->diathesimotita;
-
p->posotita=list->posotita;
-
p->next=NULL;
-
printf("Product not found. Added to the full list!\n\n");
-
}
-
else if ((cond = strcmp(list->kodikos,p->kodikos)) == 0)
-
{
-
p->posotita = p->posotita + list->posotita;
-
printf("Product found. Quantity updated!\n\n");
-
}
-
else if (cond < 0)
-
{
-
n=(struct lista *)malloc(sizeof(struct lista));
-
n->kodikos=strdup(list->kodikos);
-
n->perigrafi=strdup(list->perigrafi);
-
n->etaireia=strdup(list->etaireia);
-
n->timi=list->timi;
-
n->diathesimotita=list->diathesimotita;
-
n->posotita=list->posotita;
-
n->next=p;
-
p=n;
-
printf("Product not found. Added to the full list!\n\n");
-
}
-
else if (cond > 0)
-
{
-
p->next = insert(p->next, list);
-
}
-
-
return p;
-
-
-
}
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. ;)
4 1960
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
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. :)
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
Thanks a lot for finding the problem. :)
I'm more than happy!
Savage
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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;
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
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...
| |