问题
I have a file which the first line reads ">FileName.txt". My goal is to read this line, and save "FileName.txt" to a variable called name. So I have:
ifstream file;
/* File opening stuff */
string line, name;
getline(file,line);
stringstream converter(line);
converter >> name;
This accomplishes saving ">FileName.txt" to the variable name
, but I need to remove the '>' character. I am not sure if I should do that after this point, or if there is a way to skip over it entirely using the stringstream
.
回答1:
You can skip over it with the stream fairly easily:
char ch;
converter >> ch; // skip initial >
converter >> name; // now read the name
回答2:
You can skip over it with the function ignore
, add follow statement after stringstream converter(line);
only need one line.
converter.ignore(line.length(), '>');
来源:https://stackoverflow.com/questions/26942445/c-stringstreams-skipping-a-character