By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Duncan" <dc**@uow.edu.au> wrote in message
news:d8**************************@posting.google.c om...
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
Jul 22 '05 #2

P: n/a
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
kb******@gascad.at
Jul 22 '05 #3

P: n/a
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<40***************@gascad.at>...
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,
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.