Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code removed due to public information issues brought up in class.
Posted
Updated 8-Oct-13 5:57am
v2
Comments
pasztorpisti 6-Oct-13 16:59pm    
It is time to learn how to use a debugger. Learning this would probably be one of the best investments for the future.

1 solution

In the first code snippet, strcpy(node->name,name) leads to an access violation since node does not hold the address of any valid memory location.

[EDIT1]
Looking again at your homework code shows that you have several flawas in your code:

1) Saving and loading are not symetric in your implementation: save tries to store first the seconds and only then the name while loading tries to load first the name and then the seconds.
2) You seem to confuse sizeof() with strlen() for the name.
3) You can only use fread for reading the name if the name has always a fixed length.
4) Why do you store the data as binary file? Why not text (save: fprintf(fp, "%f %s\n", node->second, node->name);, load: fscanf(fp, "%f %s\n", &seconds, buffer);).
5) Why don't you employ the add function to add a node in the load function?
6) The load function is broken: you try to read all records from the file, but don't add each record within the while loop.
7) In save, you do not close the file in all control branches - move it out of the if block.
8) You use the list_head inconsistently: once you modify it and invalidate it as such (e.g. in the save function, while in the load function you do not reset it before loading.
9) Please try to give meaningul names from the beginning of your career: temp, plain or in any combined form, is crap. If you fail to give a decent name you probably have not understood the task at hand. Why don't you name it node_to_add or alike?

[/EDIT1]

Cheers
Andi
 
Share this answer
 
v5
Comments
diego14567 5-Oct-13 20:46pm    
Should i be assigning node the value of the last element in the linked list then doing strcpy?
Andreas Gieriet 5-Oct-13 22:11pm    
I don't know the logic of your program. If you are lucky, you get an access violation if you de-reference an uninitialized pointer (that's what -> does). Worst case, the pointer points to some process memory that holds other modifiable and accessible data... This leads to some unprdictable and very hard to find problems.

Hint: find out which node you expect at that location and make sure you have that node at hand at that location.

Cheers
Andi
Sergey Alexandrovich Kryukov 6-Oct-13 4:15am    
Good catch, but the explanation in your comment is more important; 5ed.
—SA
Andreas Gieriet 6-Oct-13 6:15am    
Hello Sergey,
thanks for your 5!
Cheers
Andi
diego14567 6-Oct-13 12:58pm    
One more question, shouldnt fread place the value of a read string inside of name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900