Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.31 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. File Stream program not showing the .exe window
  2. #include "stdafx.h"
  3. #include "fstream"
  4. #include "iostream"
  5. #include "conio.h"
  6.  
  7. using namespace std;
  8.  
  9. struct Student
  10. {
  11.     char name[40];
  12.     char grade;
  13.     float marks;
  14. public:
  15.     void getdata();
  16.     void display();
  17. };
  18.  
  19. void Student :: getdata(void)
  20. {
  21.     char ch;
  22.     cin.get(ch);
  23.     cout << "Enter name: ";
  24.     cin.getline(name, 40);
  25.     cout << "Enter grade: ";
  26.     cin >> grade;
  27.     cout << "Enter marks: ";
  28.     cin >> marks;
  29. }
  30.  
  31. void Student :: display(void)
  32. {
  33.     cout << "Name: " << name << endl;
  34.     cout << "Grade: " << grade << endl;
  35.     cout << "Marks: " << marks << endl;
  36. }
  37.  
  38.  
  39. int _tmain(int argc, _TCHAR* argv[])
  40. {
  41.  
  42.     Student arts[3];
  43.     fstream filin;
  44.     filin.open("Stu.dat", ios::in|ios::out);
  45.     if (!filin)
  46.     {
  47.         cout << "Cannot open file! n";
  48.         return 1;
  49.     }
  50.     cout << "Enter details for 3 students: ";
  51.     for (int i =0; i < 3; i++)
  52.     {
  53.         arts[i].getdata();
  54.         filin.write((char *) & arts[i], sizeof (arts[i]));
  55.     }
  56.     filin.seekg(0);
  57.     cout << "The contents of stu.dat are shown below: ";
  58.     for(int i = 0; i < 3; i++)
  59.     {
  60.         filin.read((char *) & arts[i], sizeof (arts[i]));
  61.         arts[i].display();
  62.     }
  63.     filin.close();
  64.     _getch();
  65.     return 0;
  66. }
  67.        
  68. cout << "Cannot open file! n";
  69.     _getch();
  70.     return 1;