structures fwrite

This is a discussion on structures fwrite within the C Programming forums, part of the General Programming Boards category; i dont know what is the problem, it simply dont write the struct to the file =( Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    8

    structures fwrite

    i dont know what is the problem, it simply dont write the struct to the file =(


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    struct restaurant
    	{
    	char *name;
    	};
    
    struct llist_node
    	{
    	struct restaurant *data;
    	struct llist_node *next;
    	};
    
    struct llist
    {
    	struct llist_node *head;
    };
    
    
    
    int llist_load(struct llist *list, char *path){ 
    
    	int append(struct llist *list, struct restaurant *temp);
    	struct restaurant *temp;
    	FILE *file = fopen(path,"rb");
    	
    	if (file != NULL){
    		
    		while(temp=malloc(sizeof(struct restaurant)), fread(temp, sizeof(struct restaurant), 1, file) != (size_t)NULL){
    			printf("%d",temp->name);
    			append(list,temp); 
    			
    		}
    		free(temp);
    		
    		fclose(file);
    	}
    return 1;
    }
    
    
    int llist_save(struct llist *list, char *path){
    
    	struct llist_node *node;
    	FILE *file = fopen(path,"w+");
    
    	if(list->head == NULL || file == NULL)
    		return 0;
    
    	node = list->head;
    
    	while(node != NULL){
    		printf("%s %d",*(node->data),sizeof(struct restaurant));
    		fwrite((*node).data, sizeof(struct restaurant), 1 , file);
    		node = node->next;
    	}
    
    	fclose(file);
    	return 1;
    }
    
    int main(void){
    
    	char buffer[256];
    	int append(struct llist *list, struct restaurant *temp);
    	void output(struct llist *list);
    	struct restaurant *input(void);
    	char path[] = "data.dat";
    	int option;
    	struct llist *list = malloc(sizeof(*list));
    	list->head = NULL;
    	llist_load(list,path);
    	do
    		{
    			printf("Menu\n1: Add\n2: View All\n3: Search\n4: Save\n0: Exit\n\nOption: ");
    			fgets(buffer,sizeof(buffer),stdin);
    			sscanf (buffer,"%d",&option);
    		
    			if(option == 1)
    				append(list,input());
    			else if(option ==2)
    				output(list);
    			else if (option==4)
    			{
    				if(llist_save(list, path) == 1)
    					printf("Save successful.\n");
    				else printf("Can't save.\n");
    			}
    		}
    		while(option);
    		
    	
    	return(0);
    	}
    
    int append(struct llist *list, struct restaurant *temp){
    
    	struct llist_node *node;
    	struct llist_node *new1 = malloc(sizeof(*new1));
    
    	if(list->head != NULL){
    		node = list->head;
    		while(node->next != NULL){
    			node=node->next;
    		}
    		node->next=new1;
    	}
    	else{
    		list->head=new1;
    	}
    	new1->next = NULL;
    	new1->data = temp;
    
    	return 1;
    }
    
    struct restaurant *input(void) // Introdução de novo contacto
    	{
    	char buffer[256];
    	struct restaurant *temp = malloc(sizeof(*temp));
    
    	fgets(buffer,sizeof(buffer),stdin);
    	temp->name = malloc(strlen(buffer)+1);
    	buffer[strlen(buffer)-1]=0;
    	strcpy(temp->name,buffer);
    
    	return temp;
    	}
    
    void output(struct llist *list)
    	{ 
    	struct llist_node *node;
    	node = list->head;
    	while(node != NULL){
    		printf("Name: %s\n",node->data->name);
    		node = node->next;
    	}
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    http://c-faq.com/struct/io.html
    Also, if the structure contains any pointers (char * strings, or pointers to other data structures), only the pointer values will be written, and they are most unlikely to be valid when read back in.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  3. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  4. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 08:36 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21