C++ Practical
Class Design Snippet-4
Question : Write a program that reads a file consisting of three columns of integers and called DATA.DAT
- An error message is displayed if the file cannot be opened.
- When the value of the second column is zero, display the value read for the third column, and stop reading data from the file.
- The input file stream is closed before the program code.
- The program should not use any ifstream class member functions
#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
No comments:
Post a Comment
We love to hear your thoughts about this post!
Note: only a member of this blog may post a comment.