c++中从文件读取数据失败

第一个图:创建一个文件:图片说明
第二个图:读取文件图片说明
第三个图:运行结果图片说明

并没有被成功读取,为什么啊?还有那个while循环是做什么用的?接下来为源码:
//创建文件

//创建文件

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

struct student
{
    char name[20];
    char sex;
    unsigned long birthday;
    float height;
    float weight;
};

int main()
{
    student room[4] = {
        {"Lixin", 'M', 19840318, 1.82, 65.0},
        {"Zhangmeng", 'M', 19840918, 1.75, 58.0},
        {"Helei", 'M', 19841209, 1.83, 67.1},
        {"Geyujian", 'M', 19840101, 1.70, 59.0}};

    ofstream fout("Student.txt");
    if (!fout)
    {
        cout << "文件夹打开失败!";
        return 0;
    }

    for (int i = 0; i < 4; i++)
        fout << room[i].name
             << room[i].sex
             << room[i].birthday
             << room[i].weight << endl;

    fout.close();
    return 0;
}
//调用文件

#include <fstream>
#include <iostream>
using namespace std;
struct student
{
    char name[20];
    char sex;
    unsigned long birthday;
    float height;
    float weight;
};

int main()
{
    ifstream fin("Student.txt");
    if (!fin)
    {
        cout << "文件夹打开失败!";
        return 1;
    }
    cout << "姓名\t性别\t生日\t身高\t体重" << endl;
    student S;
    while (fin >> S.name >> S.sex >> S.birthday >> S.height >> S.weight)
    {
        cout << S.name << "\t"
             << S.sex << "\t"
             << S.birthday << "\t"
             << S.height << "\t"
             << S.weight << "\t";
    }

    fin.close();
    system("pause");
    return 0;
}

首先,你存到文件里面去了,所有的类型都变成string了。所以读的时候再存需要转换一下
其次,你存的时候都没存身高。
帮你改了一下,供你测试

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

struct student
{
    char name[20];
    char sex;
    unsigned long birthday;
    float height;
    float weight;
};

void newFile() {
    student room[4] = {
        {"Lixin", 'M', 19840318, 1.82, 65.0},
        {"Zhangmeng", 'M', 19840918, 1.75, 58.0},
        {"Helei", 'M', 19841209, 1.83, 67.1},
        {"Geyujian", 'M', 19840101, 1.70, 59.0}};

    ofstream fout("Student.txt");
    if (!fout)
    {
        cout << "文件夹打开失败!";
        return;
    }

    for (int i = 0; i < 4; i++)
        fout << room[i].name << "\t"
             << room[i].sex << "\t"
             << room[i].birthday << "\t"
             << room[i].height << "\t"
             << room[i].weight << endl;

    fout.close();
}

void openFile() {
        ifstream fin("Student.txt");
    if (!fin)
    {
        cout << "文件夹打开失败!";
        return;
    }
    cout << "姓名\t性别\t生日\t身高\t体重" << endl;
    student S;
    string tmp;
    while (getline(fin, tmp))
    {
        cout << tmp << endl;
        // cout << S.name << "\t"
        //      << S.sex << "\t"
        //      << S.birthday << "\t"
        //      << S.height << "\t"
        //      << S.weight << "\t";
    }

    fin.close();
}

int main() {
    newFile();
    openFile();
    return 0;
}
    cout << "姓名\t性别\t生日\t身高\t体重" << endl;
        这个显然执行了,说明前面打开文件是没问题的
    student S;
    while (fin >> S.name >> S.sex >> S.birthday >> S.height >> S.weight)
    {
        cout << S.name << "\t"
             << S.sex << "\t"
             << S.birthday << "\t"
             << S.height << "\t"
             << S.weight << "\t";
    }
        这个循环没有执行,说明文件是空的,或者里面的数据格式不对

        首先,确认你的Student.txt是在哪个目录下,最好
        ifstream fin("Student.txt");
        这里用绝对路径,比如ifstream fin("e:\\xxx\\Student.txt");
        然后检查下文件内容是否正确。