Thread: using a structure..

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    using a structure..

    This may seem silly to most. I want to use a structure that is in another file. I will be the only one using this structure. This file is just a header file of structures. In my main file I have a method that I will want to stuff this structure (for simplicity let's call it names_a) with data from another structure (names_b) which is in another header file. Another method initially populates "name_b" with data. In a different method I want to then copy the contents from "names_a" back into "names_b". Both methods are located in the same file. Can someone please show me an example of how I can do this? Thanks in advance!
    JK

    Code:
    struct names_a {
    int number_of_names;
    char name[10];
    }
    
    struct names_b {
    int number_of_names;
    char name[10];
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Um, what's the purpose of all of this?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I am getting this data from another application. I want to read it into the first structure and then write it back out to the original structure that I received the data from. The purpose is to get the number of names and the name itself...do some stuff then write the data back out.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm quite confused what you're asking...

    If it's how to read a structure froma (binary) file then it's somethign like this:
    Code:
    struct S
    {
       ...
    };
    
    S my_struct;
    std::ifstream file("SomeFile.bin");
    
    file.read(static_cast<char*>(&my_struct), sizeof(S));
    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.

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    read a tutorial on fstream, such as this one
    http://www.daniweb.com/tutorials/tutorial6542.html

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I don't see why you need to do this structure shuffling deal you seem so bent on.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I'm stuffing "name_a" with data from a vector...this part I have verified is working correctly. The problem I'm having is when I go to stuff this data back into the vector (container) the data is no longer there...it's garbage.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    prelude, I don't think the phrase "you seem so bent on is appropriate". I may not be presenting the real issue properly. For that I apolgize and will cease to post here in the future.

  9. #9
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I'm stuffing "name_a" with data from a vector...this part I have verified is working correctly. The problem I'm having is when I go to stuff this data back into the vector (container) the data is no longer there...it's garbage.
    If you showed us some code we could help.
    prelude, I don't think the phrase "you seem so bent on is appropriate". I may not be presenting the real issue properly. For that I apolgize and will cease to post here in the future.
    There's no need to cease posting entirely, just try to keep you questions specific and to the point.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    A block of code is worth a thousand words I really can't make out what you're trying to do. Apparently you're getting this data from another application, from a file, and from a vector all at the same time. And the purpose of the code is just as unclear (to count names? of what? huuuh? ). I believe what Prelude was trying to say is judging from the information you've given us, it doesn't sound like you need to do all this structure shuffling at all - but since you haven't told us enough for us to figure out what the heck is going on, there's no way for us to know.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    OK, I sincerely apologize for not providing more (clearer description and code). I am getting the data from a vector and putting it in a local struct. I'm then inanother procedure putting this data from the local struct back into the vector. I can't post the specific code due to proprietary rights. I only was asking for someone to post an example of how this could be accomplished. I only provided the two structures as an example...obviously this is not what I am really using. I don't know if this helps or not.
    Off topic. I appreciate Prelude's posts. She is second to none with her knowledge and command of this and other languages. I was taken by surprise with her reply that's all. No hard feelings at all. Prelude used to be moree tolerant..but that was 2 1/2 years ago...time on this thread will wear you down.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    What's the nature of this vector? Is it a vector of structs? And why does the data even get removed from the vector anyway, if it's just getting put right back in? If it's a vector of the structs, then .size() should give you the number of structs stored in the vector.

    Shooting in the dark, perhaps the reason that putting the info back into the vector ends you up with garbage is because some of the data in the struct is dynamically allocated etc, and you haven't defined your own copy constructor? If in your destructor you delete some dynamic memory, then what ends up in the struct that you just pushed onto the vector will be a dangling pointer.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    vector of the same struct. the data gets put into the vector by another application. I have a local structure that matches the structure in the vector in elements and size. I read what is in the vector and put it into my local structure. I then manipulate this data and write the new results back out to the vector. Does this make sense?

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    For that I apolgize and will cease to post here in the future.
    A quick, clean hari kari, with no whimpering. Much respect.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    thanks 7stud you are most helpful! I decided to post more after reading MadCow's reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM