473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Jul 22 '05 #1
3 4746

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Row | last post by:
HI, I would first like to say its been about 3 years since looking at java im very rusty! I have to write a post it notes type applet which will function online. (reading from a flat text file)...
3
by: Daniele | last post by:
Hi, My problem is the manage of a dynamic vector in a list of structures. In the .hpp file I define the struct and the vector v. typedef struct Word { char* Name; vector<int> v; Word* Next;...
6
by: kittykat | last post by:
Hello, I am writing a program that will read each line of a file into a vector of vectors. This data will then be analysed. Here is what i have done: typedef vector<string> lines; ......
5
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
5
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information...
1
by: gdarian216 | last post by:
Okay I am writting a program that will take input from a file and store it in a string. Then the string is separated at the whitespaces and these separate strings are loaded in a vector. My problem...
18
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the...
1
by: efittery | last post by:
I need to modify the following code to handle a vector of pairs of floats. I was just thinking of casting my vector of floats into being a vector of pairs of floats. Alternately, I have...
4
by: zr | last post by:
Hi, i need to read a text file which contains a list of items, all of type ItemType, separated by whitespace and newlines. For each line, there will be a vector<ItemTypeobject that will store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.