Wednesday 8 August 2018

c++ - Problem with printing numbers from binary file

I am writing numbers from array to binary file, but somehow when I print file last number printing twice. I not sure is it problem with how I write or how I read.



This is how I write to the file



for (int k = i - 1; k>-1; k--) {
file2.write(reinterpret_cast(&arr[k]), sizeof(int));
}



This how I read and print from the file



while ( file2 ) {
int val;
file2.read(reinterpret_cast(&val), sizeof val);
cout << val << endl;
}



So, if array of numbers is 31 29 47, printing file will give



47 
29
31
31

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...