How to convert a text file into binary and vice versa?

Hello guys,
I have made the previous problem simpler to get the answer and understand it completely.
The problem is that I want to write a C++ program that converts an ordinary text file into binary and then reads that binary file and converts it to text file so that this text file equals to first text file.
I have wrote this code for it.

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
int main()
{

	 string name1 = "first", name2 = "sec", name3 = "third";
	  int j = 0, k = 0;

	  ifstream ifs(name1.c_str()); // Here I want to read from the ordinary text file (name1).
	   ifs >> j; // Now j equals to 5 because name1 contains digit 5.
	   ifs.close();

	  ofstream ofs(name2.c_str(), ios::binary);
	   ofs.write(as_bytes(j), sizeof(int)); // Here I want to write that j to name2 file in binary mode.                      
	   ofs.close();

   	  ifstream ifs1(name2.c_str(), ios::binary); // Here I want to read from that binary file (name2).
	   ifs.read(as_bytes(k), sizeof(int)); // Here I hope k becomes 5.

	  ofstream ofs1(name3.c_str());
	   ofs1 << k; // Here I want to write that k to name3 file in ordinary text mode.

	  ifs1.close();
	  ofs1.close();
	                  // Now I hope both name1 and name2 contain 5.

	  keep_window_open();
		return 0;
}


Now what the ofs.write(as_bytes(j), sizeof(int)); or ifs.read(as_bytes(k), sizeof(int)); exactly mean?

In practice, the file name1 contains digit 5 and its size is 1 byte. The name2 contains some character/sign like [] and its size is 4 bytes and name3 contains digit 0 and its size is 1 byte, why?
I before have created the name1 file in ordinary text file mode
Please don't extent the issue by giving extra info and only think around this problem. I want just understand it.
I completely appreciate your responses.
My machine is Windows 7 32-bit. My compiler is MVS 2012. And if any more info is needed just tel me.
Last edited on
In line 16 you try reading from the wrong stream.
Maybe you want to use more expressive variable names.
Why wrong stream? ifstream is for reading from both binary and ordinary text files.
Last edited on
Look at lines 15 and 16 ;)
OW, Yeah. I correct the code and post the result again here. thanks.
OK, some part of the problem is solved now. But still there is a problem.
Although the name1 and name3 contain same content (the solved part), but when the name1 is 97 name2 becomes the 'a' character. It seems like that the binary stream convert the ordinary text file's content (which is an integer digit, here 97) to its ASCII value and stores it into itself (here the 'a' character). Do you know why?
Last edited on
Name2 contains exactly what you wrote into it (0x05000000 on my Intel-machine). I guess the software you're using to display name2's contents doesn't know how to handle binary data.
Please reread my previous post.
I feel compelled to repeat myself:

Name2 contains exactly what you wrote into it (0x61000000 on my Intel-machine). I guess the software you're using to display name2's contents doesn't know how to handle binary data.

Yes, 97 is the ASCII value of a.
97 == 0x61 == 'a'
OK, thank you. But what about the question I asked just below of the code? Does any other member like to answer that please?
Last edited on
Topic archived. No new replies allowed.