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

D

Duncan

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
 
J

John Harrison

Duncan said:
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 said:
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
 
K

Karl Heinz Buchegger

Duncan said:
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.
 
D

Duncan

Karl Heinz Buchegger said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top