读取文本文件时特殊字符显示为\325等转义序列的问题求助
Hey there! Let's break down what's happening here and how to fix it. Those \325 and \322 sequences you're seeing are octal representations of non-ASCII characters—specifically, "smart quotes" (curly quotes) and curly apostrophes from your text file. Here's why this happens and how to fix it:
Why This Is Happening
Your text file is using extended ASCII characters (like curly “, ”, and ’) instead of standard ASCII straight quotes/apostrophes. When you read the file with a regular ifstream, it reads these characters as raw bytes. The cout output then converts these non-ASCII bytes into octal escape sequences (\xxx) because your console is set to expect standard ASCII by default.
Solutions to Try
1. Use Wide Characters and Proper Encoding Handling (C++11+)
If you want to properly support non-ASCII characters (not just replace them), use wide streams and set the correct encoding for both the file and console. This works especially well on Windows:
#include <iostream> #include <fstream> #include <string> #include <fcntl.h> #include <io.h> #include <locale> #include <codecvt> using namespace std; int main() { // Set console output to UTF-8 (Windows 10+ supports this) _setmode(_fileno(stdout), _O_U8TEXT); // Use wide file stream to read Unicode characters wifstream document("filename.txt"); if (!document.is_open()) { cerr << "Couldn't open file!" << endl; return 1; } // Tell the stream to convert the file's encoding (e.g., Windows-1252) to UTF-8 document.imbue(locale(document.getloc(), new codecvt_utf8<wchar_t>)); wstring word; while (document >> word) { wcout << word << endl; } return 0; }
Note: Adjust the codecvt if your file uses a different encoding (like codecvt_utf16 for UTF-16 files).
2. Replace Smart Quotes with Standard ASCII Characters
If you only need to convert those specific weird characters to standard quotes/apostrophes, you can write a simple replacement function:
#include <iostream> #include <fstream> #include <string> using namespace std; string replace_smart_chars(string word) { // Replace curly apostrophe (\325 octal = 213 decimal) with straight ' size_t pos; while ((pos = word.find('\325')) != string::npos) { word.replace(pos, 1, "'"); } // Replace left curly quote (\322 octal = 210 decimal) with straight " while ((pos = word.find('\322')) != string::npos) { word.replace(pos, 1, "\""); } // Replace right curly quote (\323 octal = 211 decimal) with straight " while ((pos = word.find('\323')) != string::npos) { word.replace(pos, 1, "\""); } return word; } int main() { ifstream document("filename.txt"); if (!document.is_open()) { cerr << "Couldn't open file!" << endl; return 1; } string word; while (document >> word) { cout << replace_smart_chars(word) << endl; } return 0; }
3. Convert the File to Plain ASCII
A quick fix without changing code: Open your filename.txt in a text editor like Notepad++, go to Encoding > Convert to ASCII, then save the file. This will replace all smart quotes with standard ASCII equivalents, though you might lose other non-ASCII characters if they exist.
内容的提问来源于stack exchange,提问作者ThisGuy

