C++ 在 Unix 编译器上清理字符串中的 Windows 换行元素

标签 c++ arrays windows string unix

我正在使用 Unix shell 编译器,需要导入 Windows .dat 文件进行输入。不幸的是,这意味着输入文件中存在用于回车的 native “\r\n”组件。

我希望通过以下方式消除这些问题:

#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 但这导致了第二个错误的重复。任何建议将不胜感激...

最佳答案

代码中的两个问题:

  1. 你是否想使用算法函数remove , 你需要添加 #include <algorithm> .
  2. 确保::remove (这是一个删除由 char * 参数命名的文件的函数)没有被拾取,使用 std::remove .

关于C++ 在 Unix 编译器上清理字符串中的 Windows 换行元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370440/

相关文章:

c++ - 为什么 random() 在 cstdlib 中工作? (Ubuntu 10.10)

java - 将 byte[] 作为单个参数传递给 vararg 方法

c++ - Win32 - 如何在工具栏中添加 slider ?

javascript返回包含特定数组项的随机数据

Javascript 文件读取器未存储在数组中

windows - 如何使用 MS-DOS 命令模拟 curl?

windows - 修改 httpd.conf 以在 EasyPHP 中通过互联网提供服务

c++ - 类似文件夹树浏览器的 Windows 资源管理器

c# - C++ dll Pinvoke函数,参数中有函数指针结构

c++ - 什么是 'shebang' 行?