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++.