-1

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 Answers1

0

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.

SoronelHaetir
  • 11,346
  • 1
  • 8
  • 18
  • I'll probably use the fin.ignore() function like Galik mentioned, just because it seems a bit quicker. Out of curiosity though, how would I read the file in binary mode, and then copy everything but the BOM to a different file? The only way I could think of to store the data is in an int, but that's base 10. – Inertial Ignorance Jan 01 '18 at 06:02