似乎没有使用ifstream fin;和fin.open(“filename”);

时间:2014-08-23 16:37:12

标签: c++ fstream ifstream ofstream

我有一个程序让我的班级从一个文件中读取信息,作为一个数组,使用一个函数,并且我的编码方式与我去年上课的方式相同,我不确定为什么它不起作用。我本来应该为此添加更多内容,但是想要弄清楚为什么它不能使用已有的东西。

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

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

0 个答案:

没有答案