Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 10:18 AM
Duncan
Guest
 
Posts: n/a
Default Populating an STL vector from a struct which reads from a text file.

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

  #2  
Old July 22nd, 2005, 10:18 AM
John Harrison
Guest
 
Posts: n/a
Default Re: Populating an STL vector from a struct which reads from a text file.


"Duncan" <dc60@uow.edu.au> wrote in message
news:d8f16e84.0404280514.1f9074a4@posting.google.c om...[color=blue]
> 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;[/color]

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


vector<group> vgroup;
[color=blue]
>
> however i cannot work out how to populate the vector from the
> structure.[/color]

group a_group;
....
vgroup.push_back(a_group);
[color=blue]
> I have never worked with structures and vectors together before but I
> know them seperatly very well.
>[/color]

john


  #3  
Old July 22nd, 2005, 10:18 AM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: Populating an STL vector from a struct which reads from a text file.

Duncan wrote:[color=blue]
>
> 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.[/color]

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
kbuchegg@gascad.at
  #4  
Old July 22nd, 2005, 10:20 AM
Duncan
Guest
 
Posts: n/a
Default Re: Populating an STL vector from a struct which reads from a text file.

Karl Heinz Buchegger <kbuchegg@gascad.at> wrote in message news:<408FB327.55E3BA6D@gascad.at>...[color=blue]
> Duncan wrote:[color=green]
> >
> > 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.[/color]
>
> 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.[/color]

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,
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.