converting between text files and binary files

Hello,

How to convert an ordinary text file into binary and how to convert that binary file back to a text file so that the first text file equals with the last text file?
A text file IS a binary file... (sort of)
The only difference between a text file and a binary file is really their uses. A text file contains human readable text, which can be edited and modified simply with a text editor. A binary file, however, contains data that only makes sense to the program, by including characters that could not normally be read (for things like multibyte characters and integer data).

Therefore, a text file is really a subset of a binary file, containing "binary data". The only difference is, that binary data happens to make human readable words.

What am I getting at here? You can't convert between a text file and a binary file, because they are the same thing.
First off, thank you for your information. Then, So bjarne stroustrup is wrong when he wants us to convert a text file into binary and then do the reverse?
Not necessarily, its possible that he has a different interpretation of what a binary and a text file is. Different people have different opinions, that is allowed. This is just my opinion on what text files and binary files are.

It is possible that he meant something different, though, such as recieving a text file of numbers (readable numbers), storing them in a binary file of actual numbers (as in, if read with a hex editor), and then taking the binary file and turning it back, e.g.

---Text File---
65

---Binary File---
A


Because the value of 65 = 'A'
Last edited on
My code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
int main() try
{
	string name1 = "bintex", name2 = "texbin", name3 = "final";

	 ifstream ifs1(name1.c_str());
	 if(!ifs1) error("Can't open file for reading.");

	 vector<int>v1, v2;
	 int i;
	 while(ifs1.read(as_bytes(i), sizeof(int)))
	 v1.push_back(i);
	 ifs1.close();

	 ofstream ofs1(name2.c_str(), ios::binary);
	 if(!ofs1) error("Can't open file for writting.");
	 for(int i=0; i<v1.size(); i++)
		 ofs1 << v1[i];
	     ofs1.close();

	 ifstream ifs2(name2.c_str(), ios::binary);
	  if(!ifs2) error("Can't open file for reading.");

    	 while(ifs2.read(as_bytes(i), sizeof(int)))
	      v2.push_back(i);
	      ifs2.close();

     ofstream ofs2(name3.c_str());
	 if(!ofs2) error("Can't open file for writting.");
	 for(int i=0; i<v2.size(); i++)
		 ofs2 << v2[i];
	     ofs2.close();

		keep_window_open();
		return 0;
}

//********************************

catch(exception& e) 
{
	cerr << e.what() << endl;
	keep_window_open();
	return 0;
}


The bintex is a c_str() text file. Which contains 19831362. After running the code, the final file contains 859387192926298674842283059942813492.
How can I fix the code to do what I want?
All files are binary.
I'll try to explain.
What is generally referred to as a text file is one in which (8 bits = 1 ASCII character) the 8th bit of each byte is not used.
The data that can be stored in 7 bits of a 'text' file is ASCII (printable) data, it is still binary, and even though only 7 bits are used, it will occupy 8 bits of space.
7 bits can hold 2^7 different or 128 different values (base 10 or decimal), enough to hold the basic character set, UTF-8 (UTS Transformation format) fits is 8 bits and is used for backwards compatibility with ASCII. UTF-16 takes two bytes per character and is still considered a 'text' format. UTF-32 4 bytes, still text.

A binary file though is a sequence of bytes, it can contain information in any base, so will take as many bits as necessary for the base in question. Packed data can cross byte boundaries and can contain even or odd number of bits for a single data item. The next data item will start on the next bit (but doesn't have to) case in point is phone call records which are a stream of bits. Some bits will sometimes be skipped for alignment sometimes not. The structure of a chunk (record) of data is what makes sense of it all.
Take for example:
1
2
3
4
5
  struct example {
    int y;
    unsigned char x;
    char z;
  }l


if this data were to be written to a file, y would take 4 bytes, x one byte (7 bits used, 8th bit ignored) and z 1 byte (8 bits used) the entire structure would occupy 6 bytes.

There's a lot more to it (BCD for one which is binary coded decimal, which can hold a two digit number in 2 'nibbles' of 1 byte) but hope this helps some.

Largins
Thank you Largins.
Your comment was good.
Now I should do that exercise because it is an exercise. For me, the issue is some complex, but I should find a solution or fix my code so that it does that work. Can you focus on my code and tell me what part must be changed/fixed?
Last edited on
No idea how to fix that code!?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    const char txt_one[] = "file.text.1" ;
    const char binary[] = "file.binary" ;
    const char txt_two[] = "file.text.2" ;

    // create a text file containing literal integers seperatoed by space
    {
        std::ofstream file(txt_one) ;
        for( int i = 0 ; i < 10 ; ++i ) file << i*i*i << ' ' ;
        file << '\n' ;
    }
    std::cout << txt_one << " contains: " << std::ifstream(txt_one).rdbuf() ;

    // read the integers into a vector
    std::vector<int> seq ;
    {
        std::ifstream file(txt_one) ;
        int i ;
        while( file >> i ) seq.push_back(i) ;
    }
    std::cout << "the vector contains: " ;
    for( int v : seq ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    // write the integers in the vector in binary form
    {
        std::ofstream file( binary, std::ios::binary ) ;
        for( int v : seq ) file.write( reinterpret_cast<const char*>(&v), sizeof(v) ) ;
    }

    // read integers from the binary file and write them into a text file
    // as literal integers seperatoed by space
    {
        std::ifstream infile( binary, std::ios::binary ) ;
        std::ofstream outfile(txt_two) ;
        int v ;
        while( infile.read( reinterpret_cast<char*>(&v), sizeof(v) ) )
              outfile << v << ' ' ;
        outfile << '\n' ;
    }
    std::cout << txt_two << " contains: " << std::ifstream(txt_two).rdbuf() ;
}

http://coliru.stacked-crooked.com/a/e3cfb4758a01c84a
Topic archived. No new replies allowed.