Thread: Can Classes be written to a file

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    12

    Can Classes be written to a file

    i am using VC6

    Can Classes be written to a file.
    And how
    I have tried using fwrite but it didn't work
    Last edited by fatta; 05-23-2002 at 02:04 AM.

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Hi fatta,

    You really have to be more specific on what exactly you want to do.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yes, you can. I haven't used VC, but that shouldn't be any difference from other compilers.
    Code:
    MyClass MyObject;
    
    ... //(Set data)
    
    FILE* MyFile;
    MyFile=fopen("MyFile.sav", "wb")
    if(MyFile != NULL)
    {
      fwrite(&MyObject, sizeof(MyClass), 1, MyFile);
      fclose(MyFile);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you do it that way and the class contains any dynamically allocated elements, then isn't it not going to work when you try to do a load because the addresses will be invalid?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    12
    Magos thanks,

    its works, no syntax errors but when i am reading back the value into another class, i am getting back garbage values.

  6. #6
    Unregistered
    Guest

    Alternatively

    if you overload the insertion operator (<<) you can save them to file just like you would print them to screen

  7. #7
    Unregistered
    Guest
    >>isn't it not going to work when you try to do a load because the addresses will be invalid?

    no, it will work because you are not saving the address, but the whole class with all info in binary mode to file. if you open it it will be jumbled garbage.. .. and you need to use the read() command to get stuff back am i not correct?

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Unregistered
    >>isn't it not going to work when you try to do a load because the addresses will be invalid?

    no, it will work because you are not saving the address, but the whole class with all info in binary mode to file. if you open it it will be jumbled garbage.. .. and you need to use the read() command to get stuff back am i not correct?
    Hmm, I'm not sure but I think that you only save what is "in" the class object, ie Variables. Any data that is "outside" (dynamically allocated) will not be saved automatically, and needs to be saved separately.

    [EDIT]
    If you have pointers in the class, their values (what they are pointing at) will be saved too (I believe). When reading the class, these pointers will most likely point at random places in memory and could cause trouble if not handled properply.
    [/EDIT]
    Last edited by Magos; 05-23-2002 at 09:15 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by fatta
    Magos thanks,

    its works, no syntax errors but when i am reading back the value into another class, i am getting back garbage values.
    Hmm, how does your reading function look like?
    Something like this should work:
    Code:
    MyClass MyObject2;
    FILE* MyFile2;
    
    MyFile2 = fopen("MyFile.sav", "rb");
    if(MyFile2 != NULL)
    {
      read(&MyObject2, sizeof(MyClass), 1, MyFile2);
      fclose(MyFile2);
    }
    Make sure you use the EXACT same class when writing and reading.
    Also notice the "rb" insteaad of "wb" so you read the file instead of attempting to write to it again.
    This might cause garbage values and other funny stuff to your files .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    VC++? Use the CArchive class to save your classes!

  11. #11
    Unregistered
    Guest
    lets say you have this:

    struct a
    {
    int * data;
    }

    and you write it to file like this:

    fout << data;

    what will happen? Your file will contain the address in data. But when you read the address back into memory from the file there may be something else stored in that memory location:

    fin >> data;

    cout << data; //will give a memory address
    cout << *data; //will give garbage, or at best unreliable data.

    Whenever you write data to file that involves pointers, whether static memory or dynamic memory, you should write the value of the data being pointed to, not it's address.

    This:
    fout << *data;

    not this:
    fout << data;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  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. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM