Handle GBK encoding for Windows version source files — detect, read, convert, and write back
Handle GBK encoding when reading or editing Windows version source files. Always use temp files for conversion — never convert in-place.
A file is considered a Windows file when ANY of the following conditions are met:
/mnt/{c,d,e,...}/{C,D,E,...}:\Linux files do not need this skill.
Read the file directly (Read tool handles it). If content appears garbled, the file is likely GBK-encoded — proceed with the conversion workflow below.
When you need to edit a Windows file, convert via temp files to avoid data loss. Never convert in-place (overwriting the original before reading).
iconv -f GBK -t UTF-8 "/path/to/file.cpp" -o "/tmp/file_utf8.cpp"
Use the Read tool on /tmp/file_utf8.cpp. Edit with the Edit/Write tool on the temp file path.
iconv -f UTF-8 -t GBK "/tmp/file_utf8.cpp" -o "/path/to/file.cpp"
Confirm the round-trip did not corrupt content:
iconv -f GBK -t UTF-8 "/path/to/file.cpp" | diff - "/tmp/file_utf8.cpp"
If iconv lacks GBK support (e.g., minimal containers), use Python:
with open("/path/to/file.cpp", "rb") as f:
content = f.read().decode("gbk")
with open("/tmp/file_utf8.cpp", "w", encoding="utf-8") as f:
f.write(content)
Read from original, write back through temp file. Only overwrite the original after the full round-trip is verified.