In class weve been shown how to read from a binary file and read from a text file but Im unsure how to write text to a binary file?

Heres my attempt so far,

Can you help?

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	
	FILE *namefile = NULL;
	char line[35];

	namefile = fopen( "s:\names.dat", "wb");

	fwrite(line, sizeof( line ),35,namefile);
	while (feof( namefile ) == 0)
	{
		cout << "Name1" << endl;
		cout << "Name2" << endl;
		cout << "Name3" << endl;
		cout << "Name4" << endl;
		cout << "Name5" << endl;
		fwrite(&line, sizeof( line ),35,namefile);
	}
	fclose( namefile );
	cin.ignore;
	cin.get();
}

Thanks for any reply.

You're lucky it doesn't compile so that you don't have an unconditional loop continually writing unknown garbage to a file.

I'd say your first step here might be to have some text to write to the file. And given your loop, I'd say skip the looping for the moment too.

What are you expecting writing text to a 'binary file' to do?

You're lucky it doesn't compile so that you don't have an unconditional loop continually writing unknown garbage to a file.

I'd say your first step here might be to have some text to write to the file. And given your loop, I'd say skip the looping for the moment too.

What are you expecting writing text to a 'binary file' to do?

Im trying to get a binary file to store 5 names, so I can then use said 5 names to be re-read and display in another program.

We were advised to use binary files instead of text because of error handling.

We were advised to use binary files instead of text because of error handling.

Hm. Perhaps you misunderstood (unless I am), because that makes little sense to me.

If you have some updated code to post, if you posted it I might be able to read it.

Hm. Perhaps you misunderstood (unless I am), because that makes little sense to me.

If you have some updated code to post, if you posted it I might be able to read it.

I probably have, the jist of what my lecturer was hinting at was that with a textfile it is more hassel to check inputs and outputs whereas (presumably according to him) with a binary file to process with reading/writing is less obstacle ridden.

Im sorry if Im not explaining myself clearly enough, its blatant I havent got a good grasp on the difference between the two.

I'll post up the two working programs I have which are both reading of a binary and a text file.

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	int no = 1;
	FILE *nofile = NULL;
	char cont = 'n';

	// read numbers from file
	if ( (nofile = fopen( "numbers.dat", "rb" )) == NULL )
	{
		cout << "Cannot open numbers file" << endl;
		exit( -1 );
	}

	cout << endl;
	cout << "Stored numbers are" << endl;
	fread( &no, sizeof( no ), 1, nofile );
	while ( feof( nofile ) == 0 )
	{
		cout << no << endl;
		fread( &no, sizeof( no ), 1, nofile );
	}
	fclose( nofile );
	cin.ignore();
	cin.get();
}
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
	FILE *txtfile;
	char line[1024];

	// open text file for read only
	if ( ( txtfile = fopen( "names.txt", "r" ) ) == NULL )
		exit (-1);

	// Prompt user
	cout << "The names in the text file are:" << endl;
	cout << endl;


	// keep reading until end of file is reached
	while (  fgets( line, 1024, txtfile ) != NULL )
		cout << line;

	// close file
	fclose( txtfile );
	cin.get();
}

I probably have, the jist of what my lecturer was hinting at was that with a textfile it is more hassel to check inputs and outputs whereas (presumably according to him) with a binary file to process with reading/writing is less obstacle ridden.

Hm. Sounds vastly oversimplified and I am still curious about the writing of text in binary mode.

Hm. Sounds vastly oversimplified and I am still curious about the writing of text in binary mode.

Can you write text to a binary file, Yes or No.

Then let this thread die.

This question has already been answered. Start a new discussion instead.