|
-
May 2nd, 2009, 02:00 PM
#2
Re: create doubly linked list with .txt file contents read each line as string
That's a big problem. That assignment is simply copying the pointer to the string, which, as we can find by travelling up the stack, is just data's value. That array is getting overwritten every iteration of the loop.
The easy way to fix this is instead of your nodes having character pointers, they have arrays instead. This'll work because you have a predefined BUFSIZ. The disadvantage to this is that now your nodes will be big (very big if BUFSIZ is large). The more correct way would be to dynamically allocate the string. Yes, this means you'll be issuing two calls to malloc, one for the node, and one for the string in the node. You'll want to use strlen (plus 1) to determine how many bytes to allocate, and strcpy to deep copy the characters into your new buffer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|