I use C++. .I have a big problem. I need to divide data from the strings and put them into the vector.In this case, let's say i have two strings and each string will end with '\n', how will i read the characters for each string without ignoring the newline?

Example of strings

"3332 2223 2224 5443" //string 1 (entity a)
"6654 5522 3432 6754" //string 2 (entity b)

Each string is representing an entity for my problem. If i use ">>", this operator will ignores the newline of these strings, and it is not suitable for my problem. (Because i need to compare and sort the data based on the strings)

Can somebody gives some suggestions on what method or how should i deal with this problem?

Use getline() to read the entire line and break it into it's pieces.

I already used getline() for myproblem. You can look to my code, may be you can some comment on it.


string x;
string n;

ifstream infile;

if (!infile)
cerr << "File could not be opened" << endl;

while(getline(infile, x, '\n' ))
istringstream ss(x);

If each line of the file contains exactly 4 pieces of information separated by whitespace (spaces or newlines or tabs, etc), then there is no reason you can't use >> to read each piece of information from the file.

while (fin >> n)
    for(i = 0; i < 3; ++i)
       fin >> n;

If the file isn't that highly structured, then reading the file line by line and parsing each line is required.