Why does this data stream end at byte 26?
I am still working on this bitmap I / O issue from a week ago. I got stuck again, so I decided to start with the I / OI type I was familiar with and make it more like what I needed relentlessly (which checks every byte (pixel) at a time and outputs to a file based on that byte ).
I started with a program that read and checked every character in a text file and output "Z" if it was above a certain threshold and output "A" if it was below it.
This program worked great, so I decided to change it from characters to bytes in the file.
Now I had problems with this. The first 26 (bytes 0-25) bytes of the file are correct, but the rest are 0 or -1, depending on whether I use ifstream.get()
or ifstream.read
.
The input file Input.FILE
was executed in a hex editor and just contains 0x00-0xFF
. It is 256 bytes long.
Code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream sourcefile;
ofstream output;
ofstream output2;
output.open("output.txt");
output2.open("messages.txt");
sourcefile.open("Input.FILE");
vector<char> data(256);
sourcefile.read(&data[0],256);
sourcefile.seekg(0,ios::beg);
for(int i = (int) 0x00 ; i < data.size() ; i++)
{
output2 << "Value data[" << i << "] = " << (int) data[i] << endl;
if((int)data[i] < 0)
{
// since I can't make an unsigned char vector, I use this to convert
// it to the correct number. Could this be the problem?
data[i] = 256 + data[i];
output2 << "less than zero." << endl;
}
if(data[i] > 64)
{
data[i] = 0xFF;
output2 << "greater than 64, set to 0xFF." << endl;
}
else if(data[i] < 64)
{
data[i] = 0x00;
output2 << "less than 64, set to 0x00." << endl;
}
else
{
// This is most likely pointless, but I didn't want to take a chance
data[i] = 0x75;
output2 << "neither greater nor less than 64? Set to 0x75." << endl;
}
output2 << endl;
}
output.write(&data[0],256);
}
Output (from message.txt
):
Note: data[0-25]
contain correct values
...
Value data [19] = 19 is less than 64, set to 0x00.
These values ββ[20] = 20 less than 64 are set to 0x00.
These values ββ[21] = 21 less than 64 are set to 0x00.
These values ββ[22] = 22 less than 64 are set to 0x00.
These values ββ[23] = 23 less than 64 are set to 0x00.
These values ββ[24] = 24 less than 64 are set to 0x00.
These values ββ[25] = 25 less than 64 are set to 0x00.
Data value [26] = 0 less than 64, set to 0x00.
source to share
If you look at the ascii code 25 you can see what that means end of medium
, so there is a good chance that if you are reading in ascii mode, any subsequent reads will not work.
Try to indicate that you are using a binary:
sourcefile.open("Input.FILE", ios::binary);
source to share