我知道如何从文件中读取输入,并且我知道如何使用strtok
来读取用户输入,但我如何将这两者结合起来呢?
例如,我想从文本文件中读取以下两行:
00003, 3342, 54329
02425, 4323, 43255
将00003,3342,54329,02425,4323,43255存储为单独的变量。
答案 0 :(得分:-1)
不要 使用strtok
,请使用std::regex
。
根据文件的大小,您可以逐行或完全在std::string
中阅读。
要read the file可以使用:
std::ifstream t("file.txt");
std::string sin((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
并进行匹配
std::regex word_regex(",\\s]+");
auto what =
std::sregex_iterator(sin.begin(), sin.end(), word_regex);
auto wend = std::sregex_iterator();
std::vector<std::string> v;
for (;what!=wend ; wend) {
std::smatch match = *what;
V.push_back(match.str());
}