run之夭夭 2020-05-22 16:09 采纳率: 0%
浏览 685
已采纳

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;
}
  • 写回答

2条回答 默认 最新

  • 胖狗子修行之路 2020-05-22 17:18
    关注

    首先,你存到文件里面去了,所有的类型都变成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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题