Garbage in vector

3

I am trying to read a file byte by byte and load each byte as an element of the vector, but for some strange reason the first two elements of the vector appear 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.txt file contains a string that says: This is a test. But when I show it on the screen it prints it to me like this: This is a test.

    
asked by M1L0 27.06.2016 в 23:03
source

1 answer

0

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 not delete the first 3 characters (do not save them) if they are:

  • ufefff - If you read everything at once
  • EF BB BF - If you read byte by byte (your case)
answered by 28.06.2016 / 16:55
source