what is wrong with this way in saving struct...,

This is a discussion on what is wrong with this way in saving struct..., within the C++ Programming forums, part of the General Programming Boards category; Code: typedef struct _INFO { char *id; int height, width; }INFO; void Save() { INFO info; char id[] = "hello"; ...

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

    what is wrong with this way in saving struct...,

    Code:
    typedef struct _INFO
    {
    	char *id;
    	int height, width;
    }INFO;
    
    void Save()
    {
    INFO info;
    
    char id[] = "hello";
    info.id = new char[sizeof(id)];
    // filling the structure
    strcpy(map.id, id);
    info.width = 640;
    info.height = 480;
    
    	
    // saving routine
    ofstream saveinfo;
    saveinfo.open("info.dat", ios::binary);
    saveinfo.write((const char *)&info, sizeof(info));
    okey when i tried to read this struct, i get garbage on the id part... why?

    thanks,

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I think it involves the fact that you are saving the value of a pointer, which may or may not still be valid when you load it again. You should probably think of some better way to save them, like either all the chars or a formatted data file.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: what is wrong with this way in saving struct...,

    Originally posted by mickey

    okey when i tried to read this struct, i get garbage on the id part... why?
    Which struct variable, map or info?

    Why did ya do this:
    Code:
    info.id = new char[sizeof(id)];
    // filling the structure
    strcpy(map.id, id);
    Is that what you meant to do, or is it a typo?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Code:
    strcpy(map.id, id);
    You cant use strcpy to copy stuff into pointers.
    Just use the mysterious = operator

  5. #5
    Unregistered
    Guest
    did you write garbage to the file in the first place?

    what happens if you write a pointer variable to a file? You write an address to the file, correct? The address is probably meaningless when you read it back in. Don't write pointers to file, write the value pointed to by the pointer to file. Then read the value back to a pertinent variable and assign the address of the variable to the pointer in the struct.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    whoops that was a typo error sorry,

    map.id is supposed to be info.id

    anyway, so that's why it doesn't work? coz i'm saving a pointer in which it points too? but before i save it, i did this,

    cout << *info.id;

    which correctly outputs the right string..., so what should i do then?

    use this id instead of the info.id to save/put it on the file?

    thanks,

  7. #7
    Unregistered
    Guest
    I would not use read() or write() to deal with the file. I would do it the old fashioned way.

    //write data to file in comma separated format
    ofstream fout(ios::app | ios binary);
    fout.open("filename.dat");
    fout << *info.id << ' ' << info.height << ' ' << info.width;

    //read data from file
    ifstream fin(ios::binary);
    fin("filename.dat");
    char dummy[80];
    fin >> dummy;
    fin >> info.height;
    fin >> info.width;

    //translate data into struct member format
    info.id = new char[strlen(dummy) + 1);
    strcpy(info.id, dummy);

    You could think about wrapping this up in overloaded << and >> operators for the struct/class if you want. You need to use C++ style structs/classes to do this though.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >map.id is supposed to be info.id

    If you make this change, your code should work fine. If not, repost.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also you may need to close the file when finished writing if you use a different stream when you read the file.

    saveinfo.close();

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    unregistered, why would you want to do it that way? that would be very tedious, using read and write would automatically put everything and read everything at once?


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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