开发者社区> 问答> 正文

c++中从文件读取数据失败问题 c++报错

第一个图:创建一个文件: image.png

第二个图:读取文件 image.png

第三个图:运行结果 image.png

并没有被成功读取,为什么啊?还有那个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;
}


展开
收起
海边一只船 2020-05-27 09:59:40 1148 0
1 条回答
写回答
取消 提交回答
  • 首先,你存到文件里面去了,所有的类型都变成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;
    }
    
    
    2020-05-27 13:40:15
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载