How can I use C++ to eliminate the BOM in a notepad .txt file? [duplicate]

北城余情 提交于 2019-12-12 07:01:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!