中替換異常引號有時從文檔中複製代碼時會得到行號和奇怪的引號。我已經寫了一個腳本來刪除這些最初的數字,但是很難找到一種方法來刪除這些奇怪的引號「」「,所以我已經包含了我的完整代碼。它讀入一個文件並放出一個格式化的文件。但編譯器警告說,這些引號是多個字符,我認爲這意味着非標準的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;
}
C++ 11確實有unicode支持。看看那個。一個出發點: http://stackoverflow.com/questions/6796157/unicode-encoding-for-string-literals-in-c11 –
我看到'while(!eof)'停止閱讀... –
爲什麼會這樣Kerrek? –