0

I am trying to convert a text file to binary, then read in that binary file and convert it back to text. This is the code I wrote for it,but the intermediate file, i.e. the binary file,"ex7_out1", is also in text,I was expecting binary encoded form of the text, how does this happen?

#include "std_lib_facilities.h"

int main()
{
    //to binary from text
    ifstream ifs{"ex7_in", ios_base::binary};
    ofstream ofs{"ex7_out1", ios_base::binary};
    char ch;
    while(ifs.get(ch)) { ofs.write(as_bytes(ch),sizeof(char)); }

    ifs.close();
    ofs.close();

    //from binary to text
    ifstream ifs1{"ex7_out1", ios_base::binary};
    ofstream ofs1{"ex7_out2"};
    char ch1;
    while(ifs1.read(as_bytes(ch1), sizeof(char))) { ofs1 << ch1; }

}

The input file "ex7_in" contains one line of text. This is an excercise from Bjarne Stroustrup's book,Programming:Principles and Practice Using C++.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Effective_cellist
  • 1,098
  • 1
  • 19
  • 39
  • 2
    What do you mean by "convert text file to binary" ? – Jarod42 Oct 03 '15 at 19:14
  • @Jarod42 I was expecting the text to be represented as binary data, not the character representation, in the intermediate file. – Effective_cellist Oct 03 '15 at 19:18
  • 2
    All data is binary. All text is encoded as numbers. Lots of software exists to decode the numbers as text because humans find that readable; it's numbers all the way down though. – Alan Stokes Oct 03 '15 at 19:22

1 Answers1

1

This can be done with std::bitset.

Format

text:

ABCD

binary-representing text:

0010000
0010001
0010010
0010011

Conversion

From text to binary:

for(auto it = std::istream_iterator<char>(ifs1); it != std::istream_iterator<char>(); ++it)
    ofs1 << std::bitset<CHAR_BIT>(*it) << std::endl; // CHAR_BIT is defined in <climits>

From binary to text:

for(auto it = std::istream_iterator<std::string>(ifs); it != std::istream_iterator<std::string>(); ++it)
    ofs << static_cast<char>(std::bitset<CHAR_BIT>(*it).to_ulong());

This is meant to be used with text-mode streams. You want to read it. We don't read binary, unless we use HEX editor to read it for us, which is the same thing I did above.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • To get characters back, I did this for the decimal representation `char ch = (char)i` ,`i` is read as `int` from the file. How should it be done for binary representation? – Effective_cellist Oct 04 '15 at 04:21
  • It does, but how to get back characters from binary representation. – Effective_cellist Oct 04 '15 at 08:04
  • `std::bitset` can be constructed from `std::string` too, like `std::bitset<8>("10101010")`, so if you'll read strings from the file (which are delimited by whitespace), you can then use `std::bitset::to_ulong` to get an `unsigned long`, which you can cast to `char`. – LogicStuff Oct 04 '15 at 08:08
  • @user3620605 I'll edit the answer if you can tell me the format of `"ex7_in"` and why do you read it with `std::ios_base::binary` flag. – LogicStuff Oct 04 '15 at 14:18
  • `"ex7_in"` is a file with just one line of text like `"I am posting this on stackoverflow"`. As I have mentioned in the question, this is an exercise from a book, to read a text file into binary and then convert that binary data to text. It was part of the exercise to read input with`" std::ios_base::binary"` flag. – Effective_cellist Oct 05 '15 at 11:26