How can I read a binary file (I don't know its type or what is stored in it) and print out the 0
s and 1
s into a text file?
#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
char ch;
bool x;
int i = 0;
if (argc < 3)
{
cout << endl << "Please put in all parameters" << endl;
return 0;
}
char *c = new char[4];
ifstream fin(argv[1], ios_base::in | ios_base::binary);
if (!fin.is_open())
{
cout << "Error opening input file" << endl;
return 0;
}
if (!strcmp(argv[2], "-file"))
{
ofstream fout(argv[3]);
if (!fout.is_open())
{
cout << "Error opening output file" << endl;
return 0;
}
while (fin.read(c, sizeof ch))
{
fout << c;
}
cout << "Contents written to file successfully" << endl;
fout.close();
}
else if (!strcmp(argv[2], "-screen"))
{
cout << endl << "Contents of the file: " << endl;
while (fin.read((char *)&x,sizeof x))
{
cout << x;
}
cout << endl;
}
else
{
cout << endl << "Please input correct option" << endl;
return 0;
}
fin.close();
return 0;
}