文件流程序未显示.exe窗口

时间:2012-02-26 11:07:52

标签: c++ exe filestream

最近我制作了一个简单的C ++程序,它使用Visual C ++ 2010 express中的File Streams write()和read()来读取和写入类对象。程序编译得很好,但是当我转到.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 个答案:

答案 0 :(得分:1)

尝试在此处_getch();

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