c++在Unix编译器上清除字符串中的Windows新行元素
C++ Scrubbing Windows New Line Elements in a String on a Unix Compiler
我正在使用Unix shell编译器,需要导入一个Windows .dat文件进行输入。不幸的是,这意味着在输入文件中存在用于回车的本地'rn'组件。
我希望用下面一行的东西来清除这些:
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("myFile.dat");
string array[100];
int i = 0;
while(getline(dataIn, str))
{
str.erase(remove(str.begin(), str.end(), 'n'), str.end());
str.erase(remove(str.begin(), str.end(), 'r'), str.end());
array[0] = str;
i++;
}
return 1;
}
但是,这会提供以下错误:
error: cannot convert ‘__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’ to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’
表示第一个erase(),后面跟着
error: request for member ‘erase’ in ‘temp.std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]()’, which is of non-class type ‘const char*’
我已经尝试了str.c_str().erase
,但这导致了第二个错误的重复。
代码中的两个问题:
- 如果要使用
remove
算法功能,需要增加#include <algorithm>
。 - 要确保
::remove
(这是一个删除由char *
参数命名的文件的函数)没有被拾取,使用std::remove
。
相关文章:
- 使用新行和不使用新行读取文件
- 遇到新行时,有没有办法停止istream_iterator
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 新行上的 clang 格式右括号
- C++流:保存到新行而不是覆盖旧行
- (C++) 使用诅咒打印从文件中读入的新行
- 按升序输出整数时,将最后一个整数放在新行上
- 用C++计算输入文本的新行数
- 为什么我的代码在终端中没有输出任何内容.开始新行没有错误
- WriteConsoleOutputCharacterW - 我的新行在哪里?
- 为什么当测试用例数为 1 时,我的程序不输入而是打印新行?
- 如何运行 while 循环直到到达新行?
- 从文本文件读取时,如何知道是否已到达新行?
- 句子转换器在存在新行的情况下不起作用
- c++在Unix编译器上清除字符串中的Windows新行元素