Программа File Stream не отображает окно .exe - PullRequest
1 голос
/ 26 февраля 2012

Недавно я создал простую программу на C ++, которая читает и записывает объекты классов с помощью File Streams write () и read () в Visual C ++ 2010 express. Программа компилируется нормально, но когда я иду к файлу .exe и открываю его, он закрывается в один миг. Я использовал '_getch ()' в конце, но у меня все еще есть эта проблема.

У меня есть опыт работы в Turbo C ++ (вините мою школу).

Вот, кстати, код.

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

1 Ответ

1 голос
/ 26 февраля 2012

Попробуйте поставить _getch(); здесь

    cout << "Cannot open file! \n";
    _getch();
    return 1;
...