I'm not sure if I'm doing this right so I want to check my code. it works but I'm not sure its working right. I need it to read the binary file, and store the 16 bit integers in an array of ints that is the exact size needed. I tried to do sizeof(storage[i])
so I could see if I was storing 16 bits but it says 32 (I'm guessing because int automatically allocates 4 bytes?
void q1run(question q){
int end;
std::string input = q.programInput;
std::ifstream inputFile (input.c_str(), ios::in | ios::binary); //Open File
if(inputFile.good()){ //Make sure file is open before trying to work with it
//Begin Working with information
cout << "In File: \t" << input << endl;
inputFile.seekg(0,ios::end);
end=inputFile.tellg();
int numberOfInts=end/2;
int storage[numberOfInts];
inputFile.clear();
inputFile.seekg(0);
int test = 0;
while(inputFile.tellg()!=end){
inputFile.read((char*)&storage[test], sizeof(2));
cout << "Currently at position" << inputFile.tellg() << endl;
test++;
}
for(int i=0;i<numberOfInts;i++){
cout << storage[i] << endl;
}
}else{
cout << "Could not open file!!!" << endl;
}
}
EDIT:::::::::::::::::::::::::::::::::::::::::::::;
I changed the read statement to:
inputFile.read((char*)&storage[test], sizeof(2));
and the array to type short
. now Its pretty well working except the output is a little strange:
In File: data02b.bin
8
Currently at position4
Currently at position8
10000
10002
10003
0
I'm not sure what's in the .bin file, but I'm guessing the 0 shouldn't be there. lol