注册 登录
编程论坛 C++教室

在 C++ 中读取文件中的数据存储在数组中,并且对数组进行排列处理

helloworldll 发布于 2014-12-01 23:02, 466 次点击
5
Franks,Tom 2 3 8 3 6 3 5
Gates,Bill 8 8 3 0 8 2 0
Jordan,Michael 9 10 4 7 0 0 0
Bush,George  5 6 5 6 5 6 5
Heinke,Lonnie  7 3 8 7 2 5 7
这是文件中的数据格式,其中数字表述的是一周内每天的工作时间。我想要把数据读入并且存储在数组当中,然后计算出每个人一周的总工作时间,根据总工作时间的长短来排列这几个人的顺序,请大家帮忙解决。

[ 本帖最后由 helloworldll 于 2014-12-2 12:25 编辑 ]
3 回复
#2
梦在何方2014-12-02 14:59
人名用string st[n]存储,工作时间用int worktime[n][7]存储,总工作时间用totaltime[n]存储并与人名一一对应,排序即可
#3
helloworldll2014-12-02 16:55
回复 2 楼 梦在何方
可是我不知道应该怎么写程序才能把数据存储在数组里,
#include <iostream>
#include <fstream>
# include <cstring>
# include <string>
using namespace std;

int main()
{
    ifstream fin;
    fin.open("empdata.txt");
    int themax;
    char name[50];
    int time[50][50];
    fin >> themax;

    while (!fin.eof())
    {
        for(int i=0;i<themax;i++)
        {
            fin>>name;
            cout << name<< "  ";
            for(int j=0;j<7;j++)
            {
                fin>>time[50][50];
                cout << time[i][j]<< " ";
            }
    }
    }
    fin.close();
    return 0;
}
这是我写的程序,不过不能把数据存在数组里,我不知道该怎么做了。
#4
梦在何方2014-12-02 17:16
将中间一部分改为
    ifstream fin;
    fin.open("empdata.txt");
    int themax;
    string name[50];
    int time[50][50];
    fin >> themax;

    while (!fin.eof())
    {
        for(int i=0;i<themax;i++)
        {
            fin>>name[i];
            for(int j=0;j<7;j++)
            {
                fin>>time[i][j];
            }
        }
    }
    fin.close();
    此时所有数据已存到name[]和time[][]中了,之后可进行其他操作
1