如何使用C ++消除记事本.txt文件中的BOM? [英] How can I use C++ to eliminate the BOM in a notepad .txt file?

查看:716
本文介绍了如何使用C ++消除记事本.txt文件中的BOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用来自库fstream的ifstream fin读取.txt文件,但是文件开头有一个导致问题的BOM。有没有办法,我可以从我的C ++程序中删除.txt文件中的BOM,以便fin可以读取它而没有任何问题?我知道我可以自己手动删除文件中的BOM,但我有多个文件正在使用,所以这需要一段时间。

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.

我的问题类似于这一个在这里,除了这个在Java中交易:

My question is similar to this one here, except this one deals in Java:

如何使记事本在没有BOM的情况下以UTF-8保存文本?

korifey的答案就是我要找的地方,他们说:

The answer from korifey is what I am looking for, where they said:

使用PushbackInputStream(in,3)

Use PushbackInputStream(in, 3)

我能在C ++中做些类似的事吗?还应该注意的是,我只有Notepad(不是Notepad ++),最好在不下载任何新软件的情况下解决我的问题。我也不想改变Notepad本身查看BOM的方式,我只是想从我的.txt文件中物理删除BOM。我正在处理的BOM是前8个字符。

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.

推荐答案

通过以二进制模式打开文件,这将是最简单的,读取所有数据并将除BOM之外的所有内容复制到另一个文件,然后删除旧文件,最后将新文件重命名为旧文件名。

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.

这篇关于如何使用C ++消除记事本.txt文件中的BOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆