2013-06-30 9 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 имеет поддержку юникода. Посмотрите на это. Начальная точка: 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

Какой сценарий вы написали, чтобы удалить ведущие номера? У вас есть доступ к sed или tr? Они существуют только для такого рода проблем.

sed -e 's/[‘’“”]//g' 

Нет необходимости заново изобретать колесо