C++ stringstreams skipping a character

老子叫甜甜 提交于 2020-01-04 02:32:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!