Thread: hi! saving a struct..., kewl! but need more info...,

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

    hi! saving a struct..., kewl! but need more info...,

    ofstream savestruct;
    savestruct.open("names.dat");
    savestruct.write((const char*), sizeof(savestruct));

    like after doing those 3 statements, everything that contains in your struct(ie, savestruct) will automatically be written to the file right? in binary form actually.

    and then, when you want to read it...,

    ifstream loadstruct;
    loadstruct.open("names.dat");
    loadstruct.read((char*)&loadstruct, sizeof(loadstruct));

    for the questions plz...,

    (1)after that, the loadstruct will be AUTOMATICALLY filled in right?? wow, this is kewl ain't it? (2)but, how? so probably, when saving a struct, it has some added info on how the struct is organize huh?

    (3)so are the graphics file formats say.., PNG, BMP, JPG, are also in someway save as structs? so if i know their struct, i could also do something like that? and automatically the struct will be filled in?

    (4)is this how someone can come up with his own file format?

    thanks!!!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hello? err.., is this a stupid questions that's why no one is replying me back to give answers?

    one more thing.., does compress bitmaps like jpg etc., before using it, MUST be decompressed?

    thanks,

  3. #3
    Unregistered
    Guest
    not a stupid question, but not basic C++ either. The number of people who know the answer will be somewhat limited.

    AFAIK the how isn't all that mysterious. The value of the various members are written and read in the order in which they are declared in the struct/class. You have to be ware of writing and reading structs with pointers as members however. In more sophisticated programs, this may be the majority of members of a struct/class.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Like the previous post said, when structures are written to file, the contents of the struct are written in the order they are declared

    Code:
    struct Mess
    {
        int scooby;
        double doo;
    }
    If you wrote this structure to file, then scooby would be written before doo. If you read this information back into memory from the file, then it's done the same way. scooby is read first, then doo is read.

    (1)after that, the loadstruct will be AUTOMATICALLY filled in right??

    yes.

    wow, this is kewl ain't it?

    erm... yeah, i guess.

    (2)but, how? so probably, when saving a struct, it has some added info on how the struct is organize huh?

    already explained.

    (3)so are the graphics file formats say.., PNG, BMP, JPG, are also in someway save as structs?

    yes and no (depends on the file format), but the are a bit more in depth. you will have to search around and read up on the file formats individually to understand how they work. BMP files are a combination of structures and binary data.

    so if i know their struct, i could also do something like that? and automatically the struct will be filled in?

    yes, but like i said, it's often not as easy as that.

    (4)is this how someone can come up with his own file format?

    That's like saying "is this how someone writes a word on a piece of paper". Coz essentially that's all you're doing.

    If you want to come up with your own file format, then you can do it any way you like. it doesn't have to be binary, it doesn't have to be ascii text, it doesn't have to have a lot of structure, it doesn't have to have structures written to it.. it's completely up to you.

    Hope this helps.
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    yes yes both of you are very very helpful thanks very much..., it feels great that i understood what am doing,

    okey..., got some more questions hope it'll be fine...,

    Unregistered:
    """"You have to be ware of writing and reading structs with pointers as members however"""""

    what exactly do you mean by this? well, i am having trouble saving/writing a pointer to a file,
    ie,
    int *ptr, num = 10;
    ptr = num = 10;
    then i will save ptr, instead of num and i get garbage..., is this what you mean that i must be aware of?


    Uraldor:
    """"yes, but like i said, it's often not as easy as that. """"
    you mean, i should lookup the file format.., then find out if there's a struct in it, then decalre manually the members of this struct, together with the exact amount of bytes etc., then use this to read the file eh? or could you explain more about that?

    also.., are compressed files, must be decompressed first before you can accessed it?

    thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM