gpt4 book ai didi

c++ - 文件流程序不显示 .exe 窗口

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:52 24 4
gpt4 key购买 nike

最近我制作了一个简单的 C++ 程序,该程序使用 Visual C++ 2010 Express 中的文件流 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;
}

最佳答案

尝试在此处放置一个_getch();

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

关于c++ - 文件流程序不显示 .exe 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9452562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com