
Untitled
By: a guest on
Apr 26th, 2012 | syntax:
None | size: 1.31 KB | hits: 17 | expires: Never
File Stream program not showing the .exe window
#include "stdafx.h"
#include "fstream"
#include "iostream"
#include "conio.h"
using namespace std;
struct Student
{
char name[40];
char grade;
float marks;
public:
void getdata();
void display();
};
void Student :: getdata(void)
{
char ch;
cin.get(ch);
cout << "Enter name: ";
cin.getline(name, 40);
cout << "Enter grade: ";
cin >> grade;
cout << "Enter marks: ";
cin >> marks;
}
void Student :: display(void)
{
cout << "Name: " << name << endl;
cout << "Grade: " << grade << endl;
cout << "Marks: " << marks << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Student arts[3];
fstream filin;
filin.open("Stu.dat", ios::in|ios::out);
if (!filin)
{
cout << "Cannot open file! n";
return 1;
}
cout << "Enter details for 3 students: ";
for (int i =0; i < 3; i++)
{
arts[i].getdata();
filin.write((char *) & arts[i], sizeof (arts[i]));
}
filin.seekg(0);
cout << "The contents of stu.dat are shown below: ";
for(int i = 0; i < 3; i++)
{
filin.read((char *) & arts[i], sizeof (arts[i]));
arts[i].display();
}
filin.close();
_getch();
return 0;
}
cout << "Cannot open file! n";
_getch();
return 1;