0

Weve been given this example to work with and Im not sure about the line 9 char cont = 'n'; - is there even a need for it? The rest of the program I understand.

#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();
}
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Run.[it]
1

Weve been given this example to work with and Im not sure about the line 9 char cont = 'n'; - is there even a need for it? The rest of the program I understand.

#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();
}

The "cont" variable is never used in the code you posted after line 9, so it's unnecessary unless this is only some snippet of a larger piece of code where cont actually IS used. But if this is the whole program, you can safely delete that line. It has no effect on anything.

Votes + Comments
Cheers!
0

Thanks mate for clearing that up, I suspected as much and yes it is the whole program.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.