Reputation: 85
Ok here's the edited version of my code along with the function that creates the list. start is initialized as a global variable and start=NULL. I also changed my fwrite. Rather than enumerate a long list of variables i used buf to get its contents(i.e name, title,type...). mygets is just a function i created to remove the \n that comes after. Same problem arises. Foreign symbols come out.
void saving()
{
FILE *out;
struct node buf;
struct node *current;
out = fopen("foo", "wb");
if(out == NULL) return;
printf("Writing to file...");
while(current != NULL)
{
fwrite(&buf, sizeof(struct node), 1, out);
}
printf("Done!\n");
fclose(out);
}
void create()
{
node *p,*q;
int item;
char ti[STRLEN],na[STRLEN],ty[6];
printf("Warrior name: ");
mygets(na);
printf("\nWarrior title: ");
mygets(ti);
printf("\nWarrior Type and Class Encoding: ");
fgets(ty,6,stdin);
p=(node *)malloc(sizeof(node));
strcpy(p->name,na);
strcpy(p->title,ti);
strcpy(p->type,ty);
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
}
}
Upvotes: 1
Views: 111
Reputation: 123
For saving a linked list into a file successfully one must start from first node(Head Node) and keep moving to further nodes until last node. in your code you did not initialise the *current to the Head node.
you should take the address of Head Node as parameter to the saving() function. so that can be used to initialise the current.
while calling the saving() function you may pass the Head Node
void main()
{
//other code...
saving(Head); //while calling the saving() method just pass the head node as parameter
//remaining code...
}
so that the *current would be initialised to Head Node(first node)
void saving(struct node *current) { FILE *out;
out = fopen("foo.txt", "wb");
if(out == NULL) return;
printf("Writing to file...");
while(current != NULL)
{
fwrite (current->name, sizeof current->name, 1, out);
fwrite (current->title, sizeof current->title, 1, out);
fwrite (current->type, sizeof current->type, 1, out);
current = current->next;
}
printf("Done!\n");
fclose(out);
}
Upvotes: 0
Reputation: 6116
As Baldrick pointed out in his comment, you have not assigned current
to the start/head node of the linked list.
The way you are doing, dereferencing a non-initialized pointer, can lead to your program crash, fortunately its not so for you.
Use:
struct node *current = head; // Or whatever is your head node name
By the way, if you writing a binary file, its better not to name it foo.txt, use binary extensions like .bin, .dat, etc. or better don't use any extension.
Upvotes: 2
Reputation: 22094
Your loop variable is unintialized. So probably there is nothing written, because it might be NULL at the time. You must initialize it to a proper value so it can enter the while loop. Using a debugger would have shown you this immediately.
Depending on your definition of node, the calls to fwrite
could also yield wrong results, but you should post it, before one can say for sure.
Upvotes: 0