问题
I want to read in a .txt file using ifstream fin from library fstream, but there is a BOM at the beginning of the file that is causing problems. Is there a way I can, from inside my C++ program, eliminate the BOM in the .txt file, so that fin can read it without any issues? I know I can manually delete the BOM in the file myself, but I have multiple files I'm working with so this will take a while.
My question is similar to this one here, except this one deals in Java:
How to make Notepad to save text in UTF-8 without BOM?
The answer from korifey is what I am looking for, where they said:
Use PushbackInputStream(in, 3)
Is there something similar I can do in C++ ? It should also be noted that I only have Notepad (not Notepad++), and it is preferable to solve my problem without downloading any new software. I also don't want to change how Notepad itself views BOMs, I just want to physically delete the BOM from my .txt file. The BOM I'm dealing with is the first 8 characters.
回答1:
This would be easiest by opening the file in binary mode, reading all the data and copying everything but the BOM to a different file, then removing the old file and finally renaming the new file back to the name of the old.
来源:https://stackoverflow.com/questions/48047404/how-can-i-use-c-to-eliminate-the-bom-in-a-notepad-txt-file