I am still very new to C++ and I'm a bit :confused: too.
I am trying to make a program which takes the ascii string from a text file and converts it into binary.
I have been able to come up with this code so far:
#include <iostream>
#include <string>
#include <iomanip>
#include <string.h>
#include<fstream>
using namespace std;
void displayBinary(unsigned);
int main()
{
string str;
fstream file_op("c:\\test_file.txt",ios::in);
while(file_op >> str)
for(int i =0; i < str.length(); i++)
{
displayBinary((unsigned)str.at(i));
}
cout << endl;
cout << endl ;
file_op.close();
return 0;
}
void displayBinary(unsigned u)
{
register int b;
for(b = 128; b > 0; b = b/2)
{
(u & b)?(cout << '1'):(cout << '0');
}
cout << " ";
}
Everything seems to work fine. I just have one problem. I want to save the output to a text file. But I dont seem to get that done.
Any ideas how will I be able to do that?