从文件中读取 C++ 不起作用
c++ reading from a file does not work
我试图从文件中读取,基本上我的文本文件是这样的;
23 4 * 19 2 - + #
6 3 - #
36 #
我试图通过忽略文件末尾的#从文件中读取它们。我不想接受#
.之后,我想将其存储在我的队列中。这是我的代码的一部分,如下所示;当我显示我的队列时,它仍然需要#
。我不知道为什么。如果你帮助我,我会很高兴
while (!myFile.eof()) {
getline(myFile, a, ' ');
if (a != str4) {
q.enqueue(a);
}
else {
cout << " " << endl;
}
}
q.display(cout);
一个简单的方法是将文本行读入字符串。 接下来找到#
符号并擦除#
之后字符串中的所有字符。
std::string test_string = "help fred # not fred.n";
const std::string::size_type position = test_string.find_first("#");
test_string.erase(position, test_string.length() - position);
std::cout << "After truncation, string is: " << test_string << "n";
您可以使用正则表达式清理数据:
std::smatch _match;
std::regex line_regex("(.*)#");
string line;
while(getline(myfile,line))
{
std::regex_search(line, _match, line_regex);
if (_match.size() > 0)
{
cout << _match[1].str() + "n";
}
}
正则表达式将忽略 # 之后的任何内容。根据文件的示例内容,输出将是
输入内容:
23 4 * 19 2 - + #
6 3 - # 23244
36 #
输出:
23 4 * 19 2 - +
6 3 -
36
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用