【发布时间】:2020-01-15 22:44:17
【问题描述】:
我正在尝试编写一个小程序来反转文本文件中的字符顺序。它有效,但它奇怪地处理撇号和其他特殊字符。
这是我的代码:
ifstream ifs {name};
if(!ifs) throw runtime_error("Couldn't open input file.");
ofstream ofs{"output.txt"};
if(!ofs) throw runtime_error("Couldn't open output file.");
string s;
for(char ch; ifs.get(ch);)
s.push_back(ch);
reverse(s.begin(), s.end());
for(char ch: s)
ofs << ch;
示例输入:
And—which is more—you’ll be a Man, my son!
示例输出:
!nos ym ,naM a eb llôÄ‚uoyîÄ‚erom si hcihwîÄ‚dnA
【问题讨论】:
-
看起来那些是 Unicode 字符。这回答了你的问题了吗? Writing Unicode to a file in C++
-
嗨,也许您的输入是 UTF-8 而不是 ascii(例如 em 破折号字符)。这可能会帮助stackoverflow.com/questions/4775437/…
-
我不知道这是否会有所作为,但请尝试使用
wchar(宽char)。