Binary codes in a txt file

Feb 25, 2011 at 7:19pm
My codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <bitset>
using namespace std;
int main(){
char *a;
ofstream file;
dosya.open("Binary codes.txt");
cout<<"U can entry text.Ur text's binary codes will save in a txt file: ";
cin>>*a;
bitset<8>binary(*a);
file<<binary;
file.close();
if(file<<binary)
cout<<"Not succ";
else
cout<<"Succ";
return 0;}


This code block in running.But takes windows error me.Why?
Feb 25, 2011 at 7:55pm
line 10 -- a is uninitialized (does not point to allocated memory). Therefore *a crashes
Feb 25, 2011 at 8:09pm
I tried string.It didn't take error.But didn't write my text's binary codes
Feb 25, 2011 at 8:10pm
If you need to get a C string (char*) from a C++ string, there is always the .c_str() member function which you could use.

-Albatross
Feb 25, 2011 at 8:26pm
Is there a reason why fstreams are not overloaded to accept C++ strings? It seems really inconsistent to me.
Feb 28, 2011 at 2:40am
Thnx Albatros.But it isn't running.
Feb 28, 2011 at 3:22am
@filipe
I've found that really annoying too... It is supposedly done in order to keep separate classes separate (reduce comconnusance). But it would be easy enough to have it automatically provide the power if both files are #included in either order...

@Helegurbann
If I understand you correctly, you want the user to be able to do something like input a byte value and output its binary representation. Examples:

'A' --> 01000001
10 --> 00001010
99 --> 01100011

Is that right?
Last edited on Feb 28, 2011 at 3:23am
Feb 28, 2011 at 12:43pm
Yes Duoas.

'A' --> 01000001
10 --> 00001010
99 --> 01100011

datas will be in a txt file.
Feb 28, 2011 at 8:51pm
OK, then you only need to read one char at a time from the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bitset>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    char c;
    ofstream file("Binary codes.txt");

    cout << "Enter a character. It's binary value will be saved to file> ";
    cin.get( c );

    bitset<8> binary( c );
    file << binary;

    file.close();
    return 0;
}

Line 8 is the single character you will get from the user.
Line 12 gets that single character.

Oh, also please be careful about how you format your code. Stay away from styles that have you stick things like { } s on the the same line as other codes. And be careful about your indentation.

Hope this helps.
Feb 28, 2011 at 9:45pm
Thnx Duoas for ur helps.But tried this method beforehand.I wanted to multichar binarycodes.And i solved my problem.Here is my codes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
#include <bitset>
#include <conio.h>
using namespace std;
int main(){
   string c;
    ofstream dosya;
dosya.open("Binary codes.txt");
cout<<"Enter ur text. It's binary value will be saved to file: ";
cin>>c;
for(int i=0;i<=c.length();i++){
bitset<8>ikili(c[i]);
dosya<<ikili;}
dosya.close();
return 0;}
Topic archived. No new replies allowed.