0

I open text file with:

std::ifstream in( "1.txt" );

if( in.good() )
{
    char ch = 0;

    while( !in.eof() )
    {
        in >> ch;

        std::cout << std::hex << (short)ch << " ";
    }
}

And I receive three strange characters at the beginning: ffef ffbb ffbf. What is it?

Igor Mironchik
  • 215
  • 1
  • 13
  • 1
    Please read [why is using eof bad](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – M.M May 08 '17 at 06:22

1 Answers1

4

It looks like a BOM marker. BOM markers are there to note that your data is UTF-8. Note that ifstream processes things like ASCII.

Best to be careful here: since you're handling it like ASCII, something might go wrong when you encounter a strange character.

See https://en.wikipedia.org/wiki/Byte_order_mark for more details about BOM markers.

atlaste
  • 27,742
  • 3
  • 52
  • 75
  • 1
    Good spotting. My only confusion is why the OP says that these bytes don't appear in their hex editor. Unless it's like the horrible Perforce diff tool I have to use at work that silently ignores BOMs. – paddy May 08 '17 at 07:16
  • 1
    @paddy Note that he actually casts a `char` to `short` as well. The data in the file is probably just the BOM markers; the extra `ff`s are just there because `char` is signed and casting will do sign extension. E.g. 0xEF will become 0xFF 0xEF [etc] – atlaste May 08 '17 at 07:19