I am reading data from a binary file in which the first number indicates the number of inputs or values to read, the second tells the length of the first input then the input itself, after that the length of the second input then the input itself and so on.
So wrote the following code:
std::ifstream infile(filename, std::ios_base::binary);
unsigned int NumberOfInputs;
infile.read((char *) &NumberOfInputs, sizeof(unsigned int));
for (unsigned int i = 0; i < NumberOfInputs; i++) {
unsigned int Input_Lengh;
infile.read((char *) &Input_Lengh, sizeof(unsigned int));
string data;
while (Input_Lengh) {
char letter;
infile.read((char *) &letter, sizeof(char));
data += letter;
Input_Lengh--;
}
}
But what if the file was corrupted and the user told me that the number of inputs is 10 and I only read 2 (Because I got to EOF for example) How may I detect this?
Aucun commentaire:
Enregistrer un commentaire