2013-06-30 54 views
4

中替換異常引號有時從文檔中複製代碼時會得到行號和奇怪的引號。我已經寫了一個腳本來刪除這些最初的數字,但是很難找到一種方法來刪除這些奇怪的引號「」「,所以我已經包含了我的完整代碼。它讀入一個文件並放出一個格式化的文件。但編譯器警告說,這些引號是多個字符,我認爲這意味着非標準的ASCII字符。它有點有用,但它不是一個很好的解決方案。任何幫助讚賞:C++如何在代碼

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

string replaceChar(string str, char ch1, char ch2); 

// Main 
int main(int argc, char *argv[]) { 

    string line; 

    fstream stri, stro; 
    // ifstream in 
    stri.open(argv[1], ios::in); 
    if(stri.fail()){ 
     cerr << "File failed to open for input" << endl; 
     return 1; 
    } 

    // ofstream out 
    stro.open("file_out.txt", ios::out); 
    if(stro.fail()){ 
     cerr << "File failed to open for output" << endl; 
     return 1; 
    } 

    // Read - Write 
    //stri.get(c); 
    getline(stri, line, '\n'); 
    while(!stri.eof()){ 
     // Remove numbers 
     line.erase(0,3); 

     //line.replace(line.begin(), line.end(), "‘", "\'"); 
     //line.replace(line.begin(), line.end(), "’", "\'"); 
     //line.replace(line.begin(), line.end(), "「", "\'"); 
     //line.replace(line.begin(), line.end(), "」", "\'"); 
     line = replaceChar(line, '‘','\''); 
     line = replaceChar(line, '’','\''); 
     line = replaceChar(line, '「','\"'); 
     line = replaceChar(line, '」','\"'); 

     stro << line << endl; 
     getline(stri, line, '\n'); 
    } 

    // Close files 
    stri.close(); 
    stro.close(); 

    // Output 
    cout << "File Edited Ok!"; 
    //cout << count -1 << " characters copied."<< endl; 
} 

string replaceChar(string str, char ch1, char ch2) { 
    for (int i = 0; i < str.length(); ++i) { 
    if (str[i] == ch1) 
     str[i] = ch2; 
    } 

    return str; 
} 
+1

C++ 11確實有unicode支持。看看那個。一個出發點: http://stackoverflow.com/questions/6796157/unicode-encoding-for-string-literals-in-c11 –

+0

我看到'while(!eof)'停止閱讀... –

+0

爲什麼會這樣Kerrek? –

回答

2

好吧,這是不漂亮,但它的作品。任何人想要完善尋找這些該死的奇怪引號之一是我的客人!

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

// Function Declaration 
bool replace(string& str, const string& from, const string& to); 

bool checkMyLine(string line); 

// Main 
int main(int argc, char *argv[]) { 

    // line to edit 
    string line; 

    fstream stri, stro; 
    // ifstream in 
    stri.open(argv[1], ios::in); 
    if(stri.fail()){ 
     cerr << "File failed to open for input" << endl; 
     return 1; 
    } 

    // ofstream out 
    stro.open("file_out.txt", ios::out); 
    if(stro.fail()){ 
     cerr << "File failed to open for output" << endl; 
     return 1; 
    } 

    // Read - Write 
    while(getline(stri, line, '\n')){ 

     // Remove numbers at start of each line followed by space, eg: "001: " 
    int i; 
    for(i = 0;i < line.length();i++) 
    { 
     if(line[i] == ' ') break; 
    } 
    line.erase(0,i+1); 

     //Replace Odd Chars 
     for(int i=0;i<line.length();i++) 
     { 
     replace(line, "\u2018","\u0027"); // replaces ‘ 
     replace(line, "\u2019","\u0027"); // replaces ’ 
     replace(line, "\u201C","\u0022"); // replaces 「 
     replace(line, "\u201D","\u0022"); // replaces 」 
     } 

     // Write to file 
     stro << line << endl; 
    } 

    // Close files 
    stri.close(); 
    stro.close(); 

    // Output Message 
    cout << "File Edited Ok!"; 
}// End of Main 
// 
bool replace(string& str, const string& from, const string& to) 
{ 
    size_t start_pos = str.find(from); 
    if(start_pos == string::npos) 
     return false; 
    str.replace(start_pos, from.length(), to); 
    return true; 
} 
0

你寫了什麼樣的腳本來刪除前導數字? 您有權訪問sedtr?它們只存在這種問題。

sed -e 's/[‘’「」]//g' 

無需重新發明輪子