我正在尝试读取格式为
的文件firstName middleName(optional) lastName petName\n
中间名出现在一半的条目上,我不确定读取这些条目并将它们放入正确的变量名中的最佳方法。任何帮助将不胜感激。
最佳答案
你可以这样做:
std::string line, word;
while (std::getline(myFile, line)) {
if (line.empty()) continue;
// read words from line:
std::istringstream is(line);
std::vector<std::string> words;
words.reserve(4);
for (int i = 0; is >> words && i < 4; i++)
words.push_back(word);
if (words.size() == 4)
// middle name was present ...
else
// it was not ...
}
关于c++ - 读取中间名可选的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19210755/