Thread: How to save into file ?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    How to save into file ?

    For example :


    Code:
    struct record {
       char name[20];
       float height;
       float weight;
    };
    
    struct record RecArray[1000];
    How can i save into file call "record.dat" ?
    And how i can load from it ?

    If the question become like this, then i know how to save and load (Since 'struct record Rec' there are no array ):
    Code:
    struct record {
       char name[20];
       float height;
       float weight;
    };
    
    struct record Rec;
    so can somebody tell me how can i save it ?
    Thanks

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    EDIT:
    Crud, I just noticed that this is the C forum. Er, just use standard fopen, fclose and fwrite instead of the streams.

    Here's an example that works with 3 records, instead of 1000, but the principle is the same. Note also I have not done any error checking to keep the code shorter.

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    
    struct record {
       char name[20];
       float height;
       float weight;
    };
    
    int main(int argc, char *argv[])
    {
      // Array of 3 records.
      struct record RecArray[3];
    
      // Fill in 1st record information
      strcpy(RecArray[0].name,"Record 1") ;
      RecArray[0].height = 1.0 ;
      RecArray[0].weight = 1.0 ;
    
      // Fill in 2nd record information
      strcpy(RecArray[1].name,"Record 2") ;
      RecArray[1].height = 2.0 ;
      RecArray[1].weight = 2.0 ;
    
      // Fill in 3rd record information
      strcpy(RecArray[2].name,"Record 3") ;
      RecArray[2].height = 3.0 ;
      RecArray[2].weight = 3.0 ;
      
      // Save all the records into a file.
      std::ofstream out( "records.dat" ) ;
      for( int i=0; i<3; i++ )
        out.write( reinterpret_cast<char*>(&RecArray[i]), sizeof(record) ) ;
      out.close() ;
      
      // Read the SECOND record from the file.
      std::ifstream in( "records.dat" ) ;
      record tempRecord ;
      in.seekg( 1*sizeof(record) ) ;
      in.read( reinterpret_cast<char*>(&tempRecord), sizeof(record) ) ;
      in.close() ;
      
      // Print out details of 2nd record.
      std::cout << "Record name:   " << tempRecord.name << "\n" ;
      std::cout << "Record height  " << tempRecord.height << "\n" ;
      std::cout << "Record weight: " << tempRecord.weight << "\n" ;
      
      std::cin.get() ;
      return 0;
    }
    Last edited by Dante Shamest; 11-14-2003 at 11:18 PM.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    18
    For standard i know it can be save and load by this way :

    Code:
    struct record {
       char name[20];
       float height;
       float weight;
    };
    
    struct record Rec;
    Code:
    /* save into file */
    fwrite(&Rec, sizeof(struct record), 1, fptr1);
    Code:
    fread(&Rec, sizeof(Rec), 1, fptr1);
    while (!feof(fptr1)) {
       printf("%s", Rec.name);
       printf("%f", Rec.height);
       printf("%f", Rec.weight);
       printf("\n");
       /* read next record */
       fread(&Rec, sizeof(Rec), 1, fptr1);
    But when the structure contain of array, i dunno how to do it :/

    Code:
    struct record {
       char name[20];
       float height;
       float weight;
    };
    
    struct record RecArray[1000];

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But when the structure contain of array, i dunno how to do it :/
    Same way

    /* write the whole array */
    n = fwrite( RecArray, 1000, sizeof(struct record), fp );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    18
    how to write record one by one ?
    not understand at all >.<

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So write &recarray[i]
    and use a for loop

    But if you're just going to write the whole array you may as well write it all in one call
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    18
    Can give an example pseudocodes ?
    So blur now ~
    >.<

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    And what pseudo-code could you possibly imagine for ONE line of code?

    You haven't even tried it yet have you
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save a file with a name in a variable?
    By guriwashere in forum C Programming
    Replies: 2
    Last Post: 06-01-2009, 04:03 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:34 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM