Hello everyone. I am having a problem with my program; it is supposed to read values from an input text file, put the data into a structure, then output the data from the structure into a binary file. The first 2 parts work fine, but I keep ending up with plain text inside my binary file.

Below is the function that is supposed to do the output. staffMem is the structure, size is the number of records in the structure, generated while getting the data from the input text file earlier.

void arrayToBinary (fstream& outfile, char *binName, staffMem s [], int size)
{
	outfile.open (binName, ios::out | ios::binary);
			
	if (!outfile)
	{
		cout << "Binary file \"" << binName
				<< "\" failed to open." << endl;
		exit (-1);
	}
	cout << "Binary file \"" << binName
			<< "\" successfully opened for writing" << endl;
			
	for (int i = 0; i < size; i++)
	{
		outfile.write (reinterpret_cast <const char *> (&s[i]), sizeof (s[i]));
	}
			
	outfile.close();
	cout << "Binary file \"" << binName
			<< "\" properly closed and created" << endl;
}

Thanks in advance.

If you copy the contents of a text file into a binary file, the text isn't magically going to become unreadable. I think your expectations are misplaced and the code is probably working fine. Though it's hard to say for sure since you've hidden all of the data behind the staffMem type.

Sorry, I had reused part of the code from what my lecturer had given as an example, so I assumed that the "outfile.open (binName, ios::out | ios::binary);" bit in the 3rd line would make the output binary. How would I go about actually output binary then, may I ask?

Also, including the structure staffMem, as well as the other function used to get data from the text file, if it would help any.

struct staffMem
{
	char givenName [MAX];
	char surname [MAX];
	char title [MAX];
	char position [MAX];
	char office [MAX];
	char phone [MAX];
	char username [MAX];
};
int textToArray (fstream& infile, char *textName, staffMem s [])
{
	infile.open (textName, ios::in);

	if (!infile)
	{
		cout << "Text file \"" << textName
				<< "\" failed to open." << endl;
		exit (-1);
	}
	cout << "Text file \"" << textName
			<< "\" successfully opened for reading" << endl;

	int i = 0;
	
	while (infile.good())
	{
		infile.getline (s[i].givenName, MAX);
		infile.getline (s[i].surname, MAX);
		infile.getline (s[i].title, MAX);
		infile.getline (s[i].position, MAX);
		infile.getline (s[i].office, MAX);
		infile.getline (s[i].phone, MAX);
		infile.getline (s[i].username, MAX);	
		
		i++;
	}
		
	infile.close();
	cout << "Text file \"" << textName
			<< "\" properly closed" << endl;

	return i;
}

I assumed that the "outfile.open (binName, ios::out | ios::binary);" bit in the 3rd line would make the output binary

You assumed correctly. The problem isn't in what you did, but what you were expecting as a result. Byte and char are one in the same, which means that if you write 'A' to a binary file, you'll see 'A' in the file. If you write a multi-byte value to the file such as an integer, then you'll see garbage when the file is opened in a text editor.

How would I go about actually output binary then, may I ask?

It depends on what you want to see. Are you expecting a 1s and 0s? Because that's not binary, it's the textual representation of binary.

If you write a multi-byte value to the file such as an integer, then you'll see garbage when the file is opened in a text editor.

I see. The example that I reused code from was dealing with integers.

Also, I am looking to see garbage in the output file, actually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.