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.