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.

example:

STUDENT TempStud;
string x;
string n;


ifstream infile;
infile.clear();
infile.open("staf83a.txt");


if (!infile)
{
cerr << "File could not be opened" << endl;
exit(1);
}






while(getline(infile, x, '\n' ))
{
istringstream ss(x);
while(ss>>n)
{
TempStud.Exam.push_back(n);
{

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)
{
    vector1.push_back(n);
    for(i = 0; i < 3; ++i)
    {
       fin >> n;
       vector1.pushc_back(n);
    }
}

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

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.