【发布时间】:2013-11-30 01:27:35
【问题描述】:
这里的基本问题。我正在读取一个文件,并将文件中的值写入无符号整数,如下所示:
// The essential stuff
std::ifstream infile(filePath.c_str());
std::getline(infile, line);
std::istringstream in(line);
char type;
in >> type;
unsigned int x, y, z, w;
in >> x >> y >> z >> w; // My question relates to this here.
问题是:获取 x、y、z 和 w 值可以正常工作,但我希望 w 是可选的。所以一行可能包含f 0 1 2 3(类型,加上4个值),或者它可能只包含f 0 1 2。
天真地,我希望 w 会等于 EOF 或类似的东西,但可惜,事实并非如此。
我可以计算行中的空格,并用它来检查w 是否存在。或者我可以使用peek 检查该行是否在 z 之后结束,但这并不是那么简单,因为我也想忽略空格(即 z 和 w 字符之间可能有 10 个空格,这应该仍然有效)。所以解决方案肯定存在,但我想知道是否有......一些更整洁的方法?大概是这样的吧?
writeTo(w);
if w is junk
disposeOf(w);
【问题讨论】:
标签: c++ string file validation