I wrote an algorithim that reads from a .txt file. We have had some complications with software we are using and now have to read in from a .bin file.
My solution so I do not have to change my entire algorithim was to convert all the data from the .bin file into decimal, then proceed to manipulate the data.
#include <fstream>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
usingnamespace std;
int bin2dec(constchar *bin, int x)
{
int result=0;
for(;*bin;bin++)
{
if((*bin!='0')&&(*bin!='1'))
return -1;
result=result*2+(*bin-'0');
if(result<=0) return -1;
}
return result;
x = result;
}
int main()
{
char * memblock;
char * memblock1;
ifstream file ("c:\\testBin\\BEandDAexp4_11postINT.bin", ios::in|ios::binary|ios::ate); // Opens file from indicated pathway
int size; // ios:: ate sets the end of file operator ios::binary ios::in, std fstream
if(file.is_open())
{
cout<< "Attempting to Read Files" << endl;
size = (int)file.tellg(); // Use's eof operator to see how large the file is.
memblock = newchar[size];
memblock1 = newchar[size];
file.seekg(0,ios::beg);
file.read(memblock, size); // Reads file using 'size' as the itterator of where to stop, set by ios::ate
cout<< "Files Read"<< endl;
cout<< "Size " << size << endl;
ofstream myFile ("c:\\testBin\\BEandDAexp4_11postINT.bin", ios::out | ios::binary);
myFile.seekp(ios::beg);
myFile.write (memblock1, size);
myFile.close();
int y = 0;
char * ted = memblock1;
for( int i =0; i<100 ; ++i )
{
ted++;
bin2dec(ted,y); // Helper Function from abover, converts bin to decimal, and sets y as the result
cout<< "Info " << y <<endl;
}
file.close();
}
delete []memblock;
delete []memblock1;
}
The problem is my out put is
1 2 3 4 5 6
Info 0
Info 0
Info 0
Info 0
Info 0
....
Can anyone figure out why I am not able to get the information?
I haven't really looked at the whole thing, but this is clearly wrong:
1 2
return result;
x = result;
The second line will never execute.
EDIT: you're not using the return value of bin2dec(). It doesn't even need two arguments. Notice that you initialize int y = 0; and then you just print it, without ever modifying its value.
You need to decide how you are going to return your result I think.
Either like this:
1 2 3 4 5 6 7 8 9 10 11 12
void bin2dec(constchar *bin, int& x) // Note the & means the resuly will be passed back
{
int result=0;
for(;*bin;bin++)
{
if((*bin!='0')&&(*bin!='1'))
return -1;
result=result*2+(*bin-'0');
if(result<=0) return -1;
}
x = result;
}
or this:
1 2 3 4 5 6 7 8 9 10 11 12
int bin2dec(constchar *bin)
{
int result=0;
for(;*bin;bin++)
{
if((*bin!='0')&&(*bin!='1'))
return -1;
result=result*2+(*bin-'0');
if(result<=0) return -1;
}
return result;
}
Also, when you want the function to set y as the result, pass it up by reference.
Line 44: You're writing trash to the file.
Line 48: You're passing trash to the converter function.
Line 49: What is the purpose of that loop?
Line 51: That's probably not doing what you want it to do.
Line 59: Move those delete [] statements into the space at Line 56.