Hi. How can i push into the same vector types of string and integer too ?

You can't! I supposed you could convert the int's to strings and push them all in as strings. What are you trying to do? I bet there is a better way.

Dave

i need to read some data from a file,lets say:

1 Dave Usa Los Angeles

so i have 1 integer and 3 string,i have some variables int number,string name,string country,string city,i want to get the data from file so that it copyes 1 to number, Dave to name ...and so on,i hope u understand.

(Please use real words, e.g. "you" vs "u" as well as capitalize the first word of a sentence. It helps keeps the forum looking professional!)

I would suggest creating a struct.

``````struct YourStruct
{
int YourInt;
string Name;
string Country;
string City;
};``````

You can populate an instance of this struct for each line of the file, then make a `vector<YourStruct>` and push each one into the vector.

Good luck,

Dave

Ohh yes,good idee. Thank you. Sorry for the writting mistakes

I am still confused now :) Lets say my file is like this:

``````1*dave*usa*los angeles
2*brian*italy*milano

And here is my code how I am reading from the text file:

``````while(!is.eof())
{
getline(is , r);
stringstream stream;
stream << r;
while(getline(stream , s , '*'))
{
v.push_back(s);
}``````

I dont know how to populate the struct,because i read one word at a time and push it in the vector,than i know that v[0] its th enumber,v[1] the name...i hope you understand my problem

You'll have to do everything for one line in one pass through the loop

``````while(there are more lines in the file)
{
Get the whole line
parse it (so you have elements 0,1,2,3
struct.Number = element 0
struct.Name = element 1
struct.Country = element 2
struct.City = element 3
AllLines.push_back(struct);
}``````

I highly recommend you use read & write methods to store these data. It will easier for your.

``````YourStruct Obj;
fstream file("test.txt",ios::in);

Yikes, that looks tough. I would definitely use the stream functionality of fstream (the >> operator). Anytime you do sizeof() it seems not very "c++" to me.

Dave

Ok,but my program parse every line,and to determine element0,element1 i need to push it in a vector,if i dont,i dont get how to determine element0,element1......?????

- Parse the string into 4 strings.
- Convert the first one to an int (using stringstream) a
- Store it in struct.Number
- The rest of them store directly in struct.XYZ

Anytime you do sizeof() it seems not very "c++" to me.

Really. sizeof is just like any other harmless operator out there.

but my program parse every line

``````vector<YourStruct> myData;
YourStruct *Obj = new YourStruct;
fstream file("test.txt",ios::in);
myData.push_back(*Obj);
delete Obj;
file.close();
//Process myData``````

> I highly recommend you use read & write methods to store these data.
If any of the members are not POD, it is undefined behavior. If the file was written by a different program, padding between struct members might not be the same and memory corruption can happen. Lastly, the file cannot be a text file because the way you recommended is a straight byte copy directly into a punned chunk of memory.

The OP's examples suggest that the file *is* a text file and not binary. Also, std::string is not a POD type, so you cannot use daviddoria's struct with read() or write().

Edward does not recommend this practice. It is much better to use serialization techniques that are safe and established in C++. The simplest of these is a record class with overloaded << and >> operators.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.