Linking Structs and File Layout


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Linking Structs and File Layout

  1. #1
    Join Date
    Nov 2007
    Posts
    18

    Linking Structs and File Layout

    Hello,

    I have a bank customer program that has 2 structs, one for customer information and another for account information. Each customer may have more than 1 account

    struct Account{
    int accountNum;
    char accountType;
    float interestRate;
    float balance;
    };
    typedef struct Account A;

    struct Customer
    {
    char id_no[3];
    int customerID;
    char name[50];
    char address[100];
    A * accArray;
    int accountid;
    int numAcct;
    int numAccount;
    int setDeleted;
    };
    typedef struct Customer C;

    I also have a random access file which will hold the customer inforamtion and I've laid it out like this:

    fstream customer_file("customer1.txt", ios::out | ios::binary);
    Customer null_part = { " ", 0, " ", " ", 0, 0, 0, 0, 0, 0.0};

    This corresponds to the Customer struct, ie, char, int, char, char int etc. Now however I have added an array to the Customer struct and I want to know how to represent this in the file layout, char, int, char ?????? int etc.

    Also how can I link the two structs?? i.e "how many accounts do you have? What is the account type, interest rate, balance??" for each account.


    thanks in advance.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    You can write the structs to the fstream object as a raw stream of bytes using the write() member function:
    Code:
    customer_file.write((char* )(&null_part), sizeof(null_part)); //need to cast the address of struct to char*
    customer_file.write((char* )(&another_part), sizeof(another_part));
    To reconstruct the data from the file, use read() in a similar way. The read() member function copies sizeof (T) bytes into a T object.
    Danny Kalev

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    you have a pointer in one of those structs and it will be wrong when you load it back from the file, you either need to write the data that the pointer contains (following all pointers in this manner) or rebuild the value when you load from the file (if this is easy to do).

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    jonnin of course is right. The pointer member should be written as is but when you read from the file, you should ignore the pointer value and allocate it later. More on this here:
    http://www.devx.com/GetHelpOn/10MinuteSolution/16988
    Danny Kalev

  5. #5
    Join Date
    May 2007
    Posts
    843
    This does not display 0000 0001 but 0000 0000.

    My code is below:

    Code:
    int main()
    {
    	int firstNumber = 1, secondNumber = 2;
    
    	ofstream OutputArchive("D:\\C++\\C++ Languages\\Serialization 1\\Test.txt", 
    		ios::binary);
    	
    
    	/*	
    		write() first parameter is const char *
    		but &firstNumber is int*. Therefore, 
    		required to convert from int* to
    		const char *.
    	*/
    	OutputArchive.write((char*)(&firstNumber), sizeof(int));
    	
    	ifstream InputArchive("D:\\C++\\C++ Languages\\Serialization 1\\Test.txt");
    
    	cout << InputArchive.read((char*)(&firstNumber), sizeof(int));
    	
    	OutputArchive.close();
    	InputArchive.close();
    
    
    	return 0;
    }
    Thanks for your help.

  6. #6
    Join Date
    May 2007
    Posts
    843
    I found the solution is close the output file first then continue with input file.

    It just display 0013FE7 and not 1.

    Thanks.

  7. #7
    Join Date
    May 2007
    Posts
    843
    Please help me.

Similar Threads

  1. Replies: 1
    Last Post: 07-30-2007, 07:00 PM
  2. structs and header files...
    By Greggle in forum C++
    Replies: 6
    Last Post: 05-03-2007, 08:35 AM
  3. Processing Data File
    By cjspsx in forum C++
    Replies: 7
    Last Post: 12-05-2006, 03:09 PM
  4. Class vs structs design issues
    By rssmps in forum C++
    Replies: 4
    Last Post: 06-12-2006, 05:31 AM
  5. Replies: 60
    Last Post: 09-13-2002, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center