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?

William Hemsworth commented: For using code tags :) +3

Thanks for using code tags on your first post :) its not often that happends. Some of your indentation is a bit strange so I will fix that up, I also removed one warning to do with signed / unsigned comparisons.

#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 (size_t 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 << " ";
}

Now that its easier to read, try reading from the file byte by byte.
Heres how you would do that and pass it to your function displayBinary.

ifstream in("test_file.txt", ios::in);
char ch;

// Assign each byte to ch
// and then pass it to the function displayBinary
while (in.get(ch)) {
	displayBinary(ch);
}

in.close();

To save it back to a file you do basically the same thing, except this time use the ofstream and use ofstream::put to write it to the file.

Try modifying the displayBinary function like this.

ofstream out("output.txt", ios::out);
void displayBinary(unsigned u)
{
	register int b;
	char bit;

	for (b = 128; b > 0; b = b/2) {
		(u & b) ? (bit = '1') : (bit = '0');
		cout << bit;
		out.put(bit);
	}
	cout << " ";
	out.put(' ');
}

Hope this helps.

Thanks for using code tags on your first post :) its not often that happends. Some of your indentation is a bit strange so I will fix that up, I also removed one warning to do with signed / unsigned comparisons.

#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 (size_t 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 << " ";
}

Now that its easier to read, try reading from the file byte by byte.
Heres how you would do that and pass it to your function displayBinary.

ifstream in("test_file.txt", ios::in);
char ch;

// Assign each byte to ch
// and then pass it to the function displayBinary
while (in.get(ch)) {
	displayBinary(ch);
}

in.close();

To save it back to a file you do basically the same thing, except this time use the ofstream and use ofstream::put to write it to the file.

Try modifying the displayBinary function like this.

ofstream out("output.txt", ios::out);
void displayBinary(unsigned u)
{
	register int b;
	char bit;

	for (b = 128; b > 0; b = b/2) {
		(u & b) ? (bit = '1') : (bit = '0');
		cout << bit;
		out.put(bit);
	}
	cout << " ";
	out.put(' ');
}

Hope this helps.

Great!!!!!! Thanx for your Help!!! You saved my day! :cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.