Thread: Saving structures that contain pointers to a file

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Saving structures that contain pointers to a file

    Hello, I would like to know if there is an efficient way to store data of structures that contain pointers, by using fwrite.
    Code:
    	
    
    struct names{
    		
    		char *first;
    		char *last;
    	};
    		
    	struct user_data{
    		
    		struct names handle;
    		char *address;
    	};
    As an example the code stated above.
    Thank you
    Last edited by BlaX; 02-27-2010 at 07:52 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm confused. Why not write it out to a file?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    You can't just simply write what is in those structres in a file.
    The pointers that are there, are pointed to strings, where they only hold the address of the beginnings of the string.
    By writing out the whole structure with fwrite, you will be writing just the address of the string.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    You could either manually write all of the fields yourself (thus defining the format of the binary file), or use existing libraries. A quick search brought up this link, though of course there are others.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM