Populating an STL vector from a struct which reads from a text file.

Discussion in 'C++' started by Duncan, Apr 28, 2004.

  1. Duncan

    Duncan Guest

    I need to populate a vector with the following struct details:\

    struct group
    {
    string groupname;
    int gid;
    list<string> members;
    };

    the text file which this is to read from is in the following form:

    root 0 root,sarah,pdg,cdf,david,sah,simon,victor,yuan,dfs,markus,andy

    the first bit is the groupname
    the number the gid
    and the names after that are the members in the STL list.

    I have so far defined the vector:

    vector<group*> vgroup;

    however i cannot work out how to populate the vector from the
    structure.
    I have never worked with structures and vectors together before but I
    know them seperatly very well.

    Any help would be great.

    Thanks in advance,

    dc
     
    Duncan, Apr 28, 2004
    #1
    1. Advertising

  2. "Duncan" <> wrote in message
    news:...
    > I need to populate a vector with the following struct details:\
    >
    > struct group
    > {
    > string groupname;
    > int gid;
    > list<string> members;
    > };
    >
    > the text file which this is to read from is in the following form:
    >
    > root 0 root,sarah,pdg,cdf,david,sah,simon,victor,yuan,dfs,markus,andy
    >
    > the first bit is the groupname
    > the number the gid
    > and the names after that are the members in the STL list.
    >
    > I have so far defined the vector:
    >
    > vector<group*> vgroup;


    This is the first mistake. Pointers make programming difficult, use this
    instead.


    vector<group> vgroup;

    >
    > however i cannot work out how to populate the vector from the
    > structure.


    group a_group;
    ....
    vgroup.push_back(a_group);

    > I have never worked with structures and vectors together before but I
    > know them seperatly very well.
    >


    john
     
    John Harrison, Apr 28, 2004
    #2
    1. Advertising

  3. Duncan wrote:
    >
    > I need to populate a vector with the following struct details:\
    >
    > struct group
    > {
    > string groupname;
    > int gid;
    > list<string> members;
    > };
    >
    > the text file which this is to read from is in the following form:
    >
    > root 0 root,sarah,pdg,cdf,david,sah,simon,victor,yuan,dfs,markus,andy
    >
    > the first bit is the groupname
    > the number the gid
    > and the names after that are the members in the STL list.
    >
    > I have so far defined the vector:
    >
    > vector<group*> vgroup;
    >
    > however i cannot work out how to populate the vector from the
    > structure.
    > I have never worked with structures and vectors together before but I
    > know them seperatly very well.


    Make it simpler.

    Suppose there is just 1 struct group object
    Also assume you have read just one line of input from the file
    Are you able to populate that group object with the data read
    in that single line?

    group TheGroup
    string TheLine;

    getline( InFile, TheLine );

    // Do you know how to work with TheLine to extract the
    // information and fill in that information into TheGroup ?

    If yes, the rest is easy.

    Just define

    vector< group > vgroup;

    and do

    vgroup.push_back( TheGroup );

    and a copy of the TheGroup object will be appended in the vector.
    You then can reuse the TheGroup object to process the next line
    from the input, etc.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Apr 28, 2004
    #3
  4. Duncan

    Duncan Guest

    Karl Heinz Buchegger <> wrote in message news:<>...
    > Duncan wrote:
    > >
    > > I need to populate a vector with the following struct details:\
    > >
    > > struct group
    > > {
    > > string groupname;
    > > int gid;
    > > list<string> members;
    > > };
    > >
    > > the text file which this is to read from is in the following form:
    > >
    > > root 0 root,sarah,pdg,cdf,david,sah,simon,victor,yuan,dfs,markus,andy
    > >
    > > the first bit is the groupname
    > > the number the gid
    > > and the names after that are the members in the STL list.
    > >
    > > I have so far defined the vector:
    > >
    > > vector<group*> vgroup;
    > >
    > > however i cannot work out how to populate the vector from the
    > > structure.
    > > I have never worked with structures and vectors together before but I
    > > know them seperatly very well.

    >
    > Make it simpler.
    >
    > Suppose there is just 1 struct group object
    > Also assume you have read just one line of input from the file
    > Are you able to populate that group object with the data read
    > in that single line?
    >
    > group TheGroup
    > string TheLine;
    >
    > getline( InFile, TheLine );
    >
    > // Do you know how to work with TheLine to extract the
    > // information and fill in that information into TheGroup ?
    >
    > If yes, the rest is easy.
    >
    > Just define
    >
    > vector< group > vgroup;
    >
    > and do
    >
    > vgroup.push_back( TheGroup );
    >
    > and a copy of the TheGroup object will be appended in the vector.
    > You then can reuse the TheGroup object to process the next line
    > from the input, etc.


    Thanks guys that helped me alot I currently have the following code
    with this problem. The idea behind it is that the find function
    allows the user to input a groupname and it then finds it and prints
    it out but where am I going wrong. I get a few errors in the find
    function about strcomp. any other sugestions as to how i could
    implement this find function would be appreiciated.

    Heres the code:

    #include <iostream>
    #include <vector>
    #include <list>
    #include <fstream>
    #include <string>
    using namespace std;

    struct group
    {
    string groupname;
    int gid;
    list<string> members;
    };

    bool find_group(group&, vector<group>, ifstream&);
    void print_group(vector<group>, group&);
    void read_group(group&, ifstream&);

    int main()
    {


    struct group mygroup;
    vector<group> vgroup;

    ifstream file("groups.txt");
    if(!file)
    {
    cout<<"File groups.txt failed to open"<<endl;
    return 0;
    }

    while(!file.eof()) //populates the vector
    {
    read_group(mygroup, file);
    vgroup.push_back(mygroup);
    }

    int n = find_group(mygroup, vgroup, file);

    if(n == true)
    {
    print_group(vgroup, mygroup);
    return 0;
    }
    else
    {
    cout << "Group could not be found!" << endl;
    return 0;
    }
    file.close();
    }

    bool find_group(group& mygroup, vector<group> vgroup, ifstream&
    outfile)
    {
    char* groupfind = NULL;

    cout << "Please enter the name of the group you wish to find: ";
    cin >> groupfind;

    while(!outfile.eof())
    {
    int result;
    result = strcmp(groupfind, mygroup);
    if(result = 0)
    {
    return true;
    }

    }

    return false;
    }

    void print_group(vector<group> vgroup, group& mygroup)
    {
    while(!vgroup.end())
    {
    cout <<"The Group Name is: " << mygroup.groupname << endl;
    cout << "The Group ID is: " << mygroup.gid << endl;
    list<char*>::iterator p = mygroup.members.begin();
    while(p != mygroup.members.end())
    {
    cout << *p << endl;
    p++;
    }
    }

    void read_group(group& mygroup, ifstream& infile)
    {
    string temp;
    infile >> mygroup.groupname;
    infile >> mygroup.gid;

    do {
    getline(infile, temp, ',');
    if(temp == "\n" && !infile.eof())
    break;
    else
    {

    mygroup.members.push_back(temp);
    }

    } while (true);
    }

    Thanks again in advance,
     
    Duncan, Apr 29, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Van Overbeke
    Replies:
    2
    Views:
    592
    Robrecht Jacques
    Jul 11, 2003
  2. Vikas
    Replies:
    0
    Views:
    836
    Vikas
    Jul 15, 2003
  3. DanielEKFA
    Replies:
    8
    Views:
    641
    DanielEKFA
    May 16, 2005
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,438
    Chris Fogelklou
    Apr 20, 2004
  5. Replies:
    8
    Views:
    1,998
    Csaba
    Feb 18, 2006
Loading...

Share This Page