0

Trying to read a file byte by byte. Why does code block 1 return "Number of bytes read 60008" (correct) and code block 2 return "Number of bytes read 54060" (incorrect)? File in question can be found here.

CODE BLOCK 1

std::ifstream input("train-labels.idx1-ubyte", std::ios::binary);

std::vector<char> bytes(
     (std::istreambuf_iterator<char>(input)),
     (std::istreambuf_iterator<char>()));

input.close();
    
std::cout << "Number of bytes read " << bytes.size() << std::endl;

CODE BLOCK 2

std::string test_labels = "train-labels.idx1-ubyte";

std::ifstream labelsFile(test_labels, std::ios::binary);
std::vector<char> bytes;

while (labelsFile) {
    char byte;
    labelsFile >> byte;
    bytes.push_back(byte);
}
std::cout << std::endl;

std::cout << "Number of bytes read " << bytes.size() << std::endl;
sandy
  • 1

0 Answers0