在文件c ++中显示内容时出错

时间:2018-03-25 23:07:33

标签: c++ arrays file structure

我正在尝试从文件中读取数据,然后将其存储到结构数组中。从文件读取时需要显示数据库。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct student 
{
    string LastName;
    string FirstName;
    int MemberID;
    int TelephoneNum;
};

int main() 
{
    student Student1;
    int count = 0;   // counter of student

    ifstream fin;
    fin.open("library_database.txt");
    if (!fin.is_open() ) 
    { 
        cerr << "File <library_database.txt> not found";  
        cin.get();  
        return 1; 
    };

    // creating array of students
    student * Students = new student[count];

    // reading student data from file
    for (int i = 0; i < count; i++) 
    {
        cin >> Students[i].LastName >> Students[i].FirstName >> 
        Students[i].MemberID >> Students[i].TelephoneNum;
    }

    fin.close();

    // display full list of students
    for (int i = 0; i < 100; i++)
    {
        cout << i + 1 << ". " << Students[i].LastName << " " << Students[i].FirstName << " "
        << Students[i].MemberID << " " << Students[i].TelephoneNum<< endl;
    }
}

当我运行程序时,我得到了 1. 0 0 2. 0 0 等等到100。

0 个答案:

没有答案