Read from text, Write to Binary

I created a text file with comma separated data and I want to read from the text file and then write to a binary file. The code compiles, but when I look at the binary file in notepad++, it's incoherent garbage. I've tried a couple of different ways, all with the same result (that's why some code is commented out).

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
48
49
50
51
52
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
using namespace std;

//enum columns{petName=0, ownerName=1, age=2, weight=3, height=4, yearsOwned=5, gender=6};
int main()
{
	fstream dataFile;
	fstream binOut;
	string theArray[12];
	int index = 0;

	dataFile.open(".\\DataFiles\\PetInfo.txt",ios::in);
	if(dataFile.fail())
	{
		cout<<"Unable to Process."<<endl;
		return 999;
	}
	binOut.open(".\\Datafiles\\petInfo.bin",ios::out|ios::binary);
    if(!binOut.is_open())
    {
        cout << "\n\nUnable to open the BIN output file.\n\n";
        return 98;
    }

	while(!dataFile.eof())
    {
			getline(dataFile, theArray[index],'\n');
			/*getline(dataFile, theArray[index][ownerName],',');
			getline(dataFile, theArray[index][age],',');
			getline(dataFile, theArray[index][weight],',');
			getline(dataFile, theArray[index][height],',');
			getline(dataFile, theArray[index][yearsOwned],',');
			getline(dataFile, theArray[index][gender],'\n');*/

			
			
			binOut.write(reinterpret_cast<char *>(&theArray),sizeof(theArray));
        	index++;
		
    }
    int maxElements=index;
    dataFile.close();
	binOut.close();
}
The problem is on line 45.

 
binOut.write(reinterpret_cast<char *>(&theArray),sizeof(theArray));

Writing std::strings like that is not going to work. std::string contains a pointer to the data and writing that pointer to file is useless.

To get a char* to the string data you can use the data() member function. To get the length of the string data you can use the length() member function.

http://www.cplusplus.com/reference/string/string/data/
http://www.cplusplus.com/reference/string/string/length/
Also, you are actually writing text in a binary file. Just that you have opened the file in binary mode doesn't mean you write in binary mode. Both the files will look same.
To really create something in binary, create a format for binary file first. Just to give you an example:

T L V T L V T L V .... (tag length value)
for example your file will have this format. Every first byte will tell you the tag (just for start it could be string (code 1) or integer (code 2))
second byte will tell you the length. so, if value says 10 and tag tells you a string (1), you are going to read 10 bytes and convert them into string.
after the value, you will have a tag again. probably this time it says integer (2) with 4 bytes. you read 4 bytes and convert those 4 bytes into an integer.

So, this is how you interpret a binary data. If you write a big chunk of text into a file which is opened as a binary file, will not make any sense because ultimately you are going to read everything in as text only.

what I am trying to say is, interpret the data as bytes, which is the actual fun of handling binary data.

Topic archived. No new replies allowed.