【发布时间】:2014-12-19 21:09:10
【问题描述】:
我正在尝试将我的文本文件中的字符 badSymbols 替换为 goodSymbols,但它为我的替换字符添加了一个字符。例如,当我将'æ' 替换为'c' 时,我得到'Ãc'。
这个简单的程序逐行读取文本文件并替换字符,然后将其写入“fileout.txt”
int main()
{
char buffer, c;
string line, filepath;
fstream filein, fileout;
char badSymbols[] = {'', 'ê', '³', 'æ', '', '¹', '¿', '£', '', '³', ''};
char goodSymbols[] = {'s', 'e', 'l', 'c', 'z', 'a', 'z', 'L', 's', 'l', ''};
string symbol;
size_t found;
cout << "Podaj sciezke pliku, w którym zamienic znaki" << endl;
cin >> filepath;
filepath = filepath + ".txt";
filein.open("/home/damian/jezykc++/untitled2/gared.txt", ios:: in );
fileout.open("/home/damian/jezykc++/untitled2/fileout.txt", ios::trunc | ios::out);
if (filein.good() == true)
{
std::cout << "Uzyskano dostep do pliku!" << std::endl;
while (getline(filein, line))
{
// buffer=line;
//getline(filein,line);
for (int i = 0; i < sizeof(badSymbols); i++)
{
symbol = goodSymbols[i];
found = line.find(badSymbols[i]);
if (found != std::string::npos)
line.replace(found, 1, symbol);
}
cout << line << endl;
fileout << line << endl;
}
}
else std::cout << "Dostep do pliku zostal zabroniony!" << std::endl;
return 0;
}
【问题讨论】:
-
您是否排除了与编码、宽字符有关的问题?
-
这几乎肯定是一个编码问题。这是(只有一个)参考stackoverflow.com/questions/17648966/…
标签: c++