C++ 不';t似乎正在使用ifstream fin工作;和fin.open(“文件名”);

C++ 不';t似乎正在使用ifstream fin工作;和fin.open(“文件名”);,c++,fstream,ifstream,ofstream,C++,Fstream,Ifstream,Ofstream,我有一个程序,让我的类使用一个函数从一个文件中读取信息,作为一个数组,我用去年的类代码编写了这个程序,我不知道为什么它不起作用。我还想添加更多内容,但我想试着弄清楚为什么它不能使用它已经拥有的功能 我不认为它正在读取文件,因为输出文件或运行时弹出的窗口上没有显示任何内容 #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespac

我有一个程序,让我的类使用一个函数从一个文件中读取信息,作为一个数组,我用去年的类代码编写了这个程序,我不知道为什么它不起作用。我还想添加更多内容,但我想试着弄清楚为什么它不能使用它已经拥有的功能

我不认为它正在读取文件,因为输出文件或运行时弹出的窗口上没有显示任何内容

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int maxs = 50;
struct stype
{
    int crn;
    string name;
    int crhrs;
    int numstu;
    int stucrhrs;
    string prof;
};
stype initrec = { 0.0, "course", 0.0, 0.0, 0.0, "prof" };

void initem(stype p[], int &numc)
{
    int i;
    for (i = 0; i < maxs; i++) p[i] = initrec;
    numc = 0;
}
void readem(stype p[], int &numc)
{
    int i = 0;
    ifstream fin;
    fin.open("program1.dat");
    while (!fin.eof())
    {
        fin >> p[i].crn >> p[i].name >> p[i].crhrs >> p[i].numstu >> p[i].stucrhrs   >> p[i].prof;
        i++;
    }
    numc = i;
    cout << "readem reached " << endl;
}
void printem(stype p[], int &numc, ofstream &fout)
{
    int i;
    for (i = 0; i < numc; i++)
    {
        fout << left << setw(10) << p[i].crn << left << setw(15) << p[i].name << left
            << setw(15) << p[i].numstu << right << setw(2) << p[i].crhrs << right <<
            setw(2) << p[i].stucrhrs << right << setw(10) << p[i].prof << endl;
    }
    cout << "printem reached " << endl;

}
void swapem(stype &a, stype &b)
{
    stype temp;
    temp = a;
    a = b;
    b = temp;
    cout << "swapem reached " << endl;
}
void sortem()
{
    cout << "sortem reached " << endl;
}
void getaverage()
{
    cout << "getaverage reached " << endl;
}
void credithours()
{
    cout << "Credit hours totalled. " << endl;
}
void main()
{
    int crn[maxs], crhrs[maxs], stucrhrs[maxs];
    string name[maxs], prof[maxs];
    stype p[maxs];
    int numc;
    ofstream fout;
    fout.open("program1.out");
    fout.setf(ios::fixed);
    fout.precision(2);
    initem(p, numc);
    readem(p, numc);
    printem(p, numc, fout);
    getaverage();
    sortem();
    printem(p, numc, fout);
    system("pause");
}
#包括
#包括
#包括
#包括
使用名称空间std;
常数int maxs=50;
结构类型
{
int crn;
字符串名;
int crhrs;
int numstu;
int STUCHRS;
弦教授;
};
stype initrec={0.0,“课程”,0.0,0.0,0.0,“教授”};
void initem(stype p[],int和numc)
{
int i;
对于(i=0;i>p[i].crn>>p[i].名称>>p[i].crhrs>>p[i].numstu>>p[i].STUCHRS>>p[i].教授;
i++;
}
numc=i;

你是否应该有一些东西来检查文件是否正确打开。首先,不要在(!fin.feof(…)
时执行
,它将不会按照你的意愿工作。其次,你需要检查文件是否确实正确打开。你可以通过将输入操作作为循环条件来修复这两个问题(如
while(fin>>…)
)。我刚刚尝试使用
而(fin>>p[I].crn>>p[I].name>>p[I].crhrs>>p[I].numstu>>p[I].stuchrs>>p[I].prof)
仍然不起作用。窗口会显示我的cout语句,但输出文件没有任何变化。
ifstream-fin(“filename.ext”);if(!fin)抛出std::runtime\u错误(“无法打开filename.ext”);
使用此选项确保程序成功打开文件(输出文件同上)。请注意,除非捕获异常,否则这将导致异常终止,因此无法移植的
系统(“暂停”)
无法继续打开终端窗口。只是让它在文件中读取,我试图读取的p[I]比文件中的多,但现在它显示在输出文件中!我必须进入并开始执行计算功能,但至少它现在读取了文件。耶!
void readem(stype p[],int&numc){int i=0;ifstream fin;fin.open(“program11.dat”);while(!fin.eof()){fin>>p[i].crn>>p[i].name>>p[i].crhrs>>p[i].numstu>>p[i].prof;i++;}numc=i;cout