我想逐行读取txt文件,读取每一行后,我想根据选项卡“\t”分割该行并将每个部分添加到结构中的元素中。
我的结构是 1*char 和 2*int
struct myStruct
{
char chr;
int v1;
int v2;
}
其中 chr 可以包含多个字符。
一行应该是这样的:
randomstring TAB number TAB number NL
Try:
注意:如果 chr 可以包含多于 1 个字符,则使用字符串来表示。
std::ifstream file("plop");
std::string line;
while(std::getline(file, line))
{
std::stringstream linestream(line);
std::string data;
int val1;
int val2;
// If you have truly tab delimited data use getline() with third parameter.
// If your data is just white space separated data
// then the operator >> will do (it reads a space separated word into a string).
std::getline(linestream, data, '\t'); // read up-to the first tab (discard tab).
// Read the integers using the operator >>
linestream >> val1 >> val2;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)