Garbage in vector

I am trying to read a file byte by byte and go loading each byte as an element of the vector, but for some strange reason the first two elements of the vector appear to me as strange characters (garbage).

#include <iostream>
#include <fstream>
#include <vector>

void Cifrado(std::string nombre) 
{
    std::ifstream archivo;
    archivo.open(nombre.c_str(),std::ios::in|std::ios::binary);
    std::vector<char>bites;

    if(!archivo)
    {
        std::cout<<"No se pudo abrir el archivo: "<<nombre<<std::endl;
        return;
    }

    char bite;

    while(!archivo.eof() && archivo.get(bite))
    {
        bites.push_back(bite);
    }

    for(unsigned int b = 0; b <= bites.size(); b++)
    {
        std::cout<<bites[b]<<std::endl;
    }

    std::cout<<bites.size()<<std::endl;

    archivo.close();

}


int main()
{
    Cifrado("texto.txt");
    return 0;
}

The text file.txt contains a string that says like this: This is a test. But when I show it by screen it prints it to me like this: esto this is a test.

 3
Author: Pako LordPakus, 2016-06-27

1 answers

How do I comment PaperBirdMaster this is the BOM that contains the file at the beginning.

To always read the file well regardless of whether it has BOM or you could delete the first 3 characters (not save them) if these are:

  • ufefff - if you read everything at once
  • EF BB BF-Si reads byte by byte (your case)
 0
Author: Miquel Coll, 2017-04-13 13:00:52