【发布时间】:2015-04-10 12:01:44
【问题描述】:
我很难用我的母语中的一些变音符号替换我文件中的一些字符;如:
character_to_replace replacement
º ș
ª Ș
þ ț
Þ Ț
我找到了 character_to_replace 的 Unicode,但由于某种原因,该文件不会保存到预期的输出。我发现这与 UTF-8 和 unicode 转换有关。但是,当我尝试写入文件时,我设法打印出字符,但仅打印到控制台,但它不起作用。这是我的代码:
void replace(string &source, string to_replace, string replacement)
{
int found = 0;
string auxiliar;
auxiliar = source;
while (found != string::npos)
{
found = auxiliar.find(to_replace);
if (found != -1)
{
source.replace(found, 1, replacement);
auxiliar = auxiliar.substr(found + to_replace.size());
}
}
}
int main()
{
cout << endl;
string line;
ifstream file;
ofstream send_line;
send_line.open("out.txt");
file.open("in.txt");
while (!file.eof())
{
getline(file, line);
replace(line, "\u00b0", "\u0219");
replace(line, "\u00aa", "\u0218");
replace(line, "\u00fe", "\u021b");
replace(line, "\u00de", "\u021a");
send_line << line << "\n";
}
file.close();
send_line.close();
}
你能指出我可以解决这个问题的正确方向吗?谢谢。
【问题讨论】: