2015-07-12 56 views
0

我正在使用Unix shell編譯器並需要導入Windows .dat文件以進行輸入。不幸的是,這意味着在輸入文件中存在用於回車的原生'\ r \ n'組件。C++在Unix編譯器中清理Windows中的新行元素

我希望沿着以下的線,東西擦洗這些了:

#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*)’ 

爲先擦除()其次是

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但這導致了第二個錯誤的重複。任何建議,將不勝感激...代碼

+2

(dos2unix'有什麼問題?) –

+1

也許使用'std :: remove',因爲':: remove'是一個刪除文件的函數,而不是'std :: remove'函數''.. –

+0

不幸的是,這是一個教授一直堅持記事本編輯的任務。 :( – Miller

回答

1

兩個問題:

  1. 你,如果你想使用的算法功能remove,你需要添加#include <algorithm>
  2. 爲了確保::remove(這是一個刪除由char *參數命名的文件的函數)未被拾取,請使用std::remove