0

Hello!
I have small problem. I have char array which contains e.g. "10010010100010110" and I want to read/write it from/to file in binary mode. How can I convert my char array to a group of bits and how to write/read it to/from file?

I need something like this:
1. I have char array = "101010" => convert to bits - 101010 => write bits to file in binary mode
2. Read bits from file => convert to char array.

Thanks in advance!

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by elektro123
0

you don't have to convert anything -- just write the array to a file as it is. BTY: you can not write bits to a file -- only bytes.

0

Like this?:

void save()
{
 char a[] ={"101001"};
     
     ofstream file("test.bin", ios::out | ios::binary);
     file.write(a,strlen(a));   
     file.close();
      
}

Because I don't think so... My file should contain "Ő" (or similar), but it cointais 1001010...

What am I doing wrong?

0

Looks like what you really want is to convert that string into an integer then write the binary value of the integer. See this link for more info about strtol()

char num[] = "01011";
char* ptr;
int x = strtol(num,&ptr,2);
out.write((char*)&x,sizeof(int));

Edited by Ancient Dragon: n/a

0

Looks like what you really want is to convert that string into an integer then write the binary value of the integer. See this link for more info about strtol()

char num[] = "01011";
char* ptr;
int x = strtol(num,&ptr,2);
out.write((char*)&x,sizeof(int));

Thanks You very much! :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.