Sunday, 25 June 2017

CBSE Class 12 - Computer Science - C++ Practical Class Design Snippet-4 (#cbseNotes)

C++ Practical
Class Design Snippet-4

CBSE Class 12 - Computer Science - C++ Practical Class Design Snippet-4 (#cbseNotes)

Question :  Write a program that reads a file consisting of three columns of integers and called DATA.DAT

The file does not contain column labels and your application should meet these requirements:
  1. An error message is displayed if the file cannot be opened.
  2. When the value of the second column is zero, display the value read for the third column, and stop reading data from the file.
  3. The input file stream is closed before the program code.
  4. The program should not use any ifstream class member functions

Answer:



#include <iostream.h>
#include <fstream.h>
#include <process.h>
#include <conio.h>

int main()
{
	clrscr();
	ifstream infile("DATA.DAT", ios::in);
	if (!infile)
	{
		cout << "Error opening file DATA.DAT. Exiting..." << endl;
		exit(1);
	}
	
	int i1, i2, i3;
	while (infile >> i1 >> i2 >> i3)
	{
		if (i2 == 0)
			{
			cout << i3 << endl;
			break;
			}
		cout << i1 << " " << i2 << " " << i3 << endl;
	}
	infile.close();
	getch();
	return 0;
}


Data.dat File content:

1  2  3
4  5  6
7  8  9
9  2  1
8  0  4
8  3  3

CBSE Class 12 - Computer Science - C++ Practical Class Design Snippet-4 (#cbseNotes)


No comments:

Post a Comment

Note: only a member of this blog may post a comment.