just to understand how to read correctly, how could i read the next text from file, if i want to read the diferent strings in each line. Each line can have different sizes (1st line could have 3 strings and 2nd line could have 100 strings)
2 //Number of lines
A AS BPST TRRER
B AS BP
I tried in my code something like this, but i dont know how to check if program it's in the end of line.
ifstream fich("thefile.txt");
fich >> aux; //Contain number of line
for(int i=0;i<aux;i++){ //For each line
string line;
getline(fich, line);
char nt; //First in line it's always a char
fich >> nt;
string aux;
while(line != "\n"){ //This is wrong, what expression should i use to check?
fich >> aux;
//In each read i'll save the string in set
}
}
So at the end, i want that set contains: {{A,AS,BPST,TRRER} {B,AS,BP}}
Thanks.
while(line != "\n"){ //This is wrong, what expression should i use to check?
Yes, because the '\n'
was removed by the getline()
function.
Using std::istringstream
it is easy to parse an arbitrary number of words up to the end of the current line
:
string aux;
std::istringstream iss(line);
while(iss >> aux) {
// ...
}
Also note:
fich >> aux; //Contain number of line
will leave you with an empty line read with std::getline()
because in this case the '\n'
will be leftover from that operation (see Using getline(cin, s) after cin for more detailed information).