Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输入文件中的数组 我是C++初学者,现在需要处理输入文件中的数据,这些文件有如下的行:_C++_Arrays_Parsing - Fatal编程技术网

输入文件中的数组 我是C++初学者,现在需要处理输入文件中的数据,这些文件有如下的行:

输入文件中的数组 我是C++初学者,现在需要处理输入文件中的数据,这些文件有如下的行:,c++,arrays,parsing,C++,Arrays,Parsing,2012019109 Proadan Legeaf Coaa女65 这些是学生编号、姓名(2或3个单词)、性别和考试成绩 我必须为每个属性创建一个数组。此外,我不知道输入文件可能包含多少行(最多100000行),但输入文件中的第一个数字将是该文件中的行数 一旦我设置了一个数组,我需要实现一个函数,按照名称的字母字符串顺序(升序)对记录进行排序,然后将它们放入一个输出文件中 我尝试过用以下方法来完成第一部分(设置阵列),但似乎是错误的: ifstream fin; ofstream fout;

2012019109 Proadan Legeaf Coaa女65

这些是学生编号、姓名(2或3个单词)、性别和考试成绩

我必须为每个属性创建一个数组。此外,我不知道输入文件可能包含多少行(最多100000行),但输入文件中的第一个数字将是该文件中的行数

一旦我设置了一个数组,我需要实现一个函数,按照名称的字母字符串顺序(升序)对记录进行排序,然后将它们放入一个输出文件中

我尝试过用以下方法来完成第一部分(设置阵列),但似乎是错误的:

ifstream fin;
ofstream fout;

fin.open("input.txt");
fout.open("output.txt");

if (fin.fail()) {
    cout << "Fail to open inout.txt" << endl;
    exit(1);
}

int x; 
fin >> x; //this is the first number within the input text file, indicating the number of lines in the file. I would use this to determine the size of the arrays:

int UID [x];
string name [x];
string gender [x];
int score [x];

int y = 0;


// In the following part, I am trying to extract the information into the different arrays, one by one, increasing the number of the element from 0 up till x. Complier error says no matching function for call for the UID and score lines.

while (y!=x, y++) {
    getline(fin, UID [y], '\t');
    getline(fin, name [y], '\t');
    getline(fin, gender [y], '\t');
    getline(fin, score [y], '\t');
    break;
    }`
ifstream-fin;
流式流量计;
fin.open(“input.txt”);
fout.open(“output.txt”);
if(fin.fail()){
cout x;//这是输入文本文件中的第一个数字,表示文件中的行数。我将使用它来确定数组的大小:
int-UID[x];
字符串名[x];
字符串性别[x];
整数分数[x];
int y=0;
//在接下来的部分中,我将尝试将信息逐个提取到不同的数组中,将元素的数量从0增加到x。编译器错误表示没有用于调用UID和分数线的匹配函数。
而(y!=x,y++){
getline(fin,UID[y],'\t');
getline(fin,名称[y],'\t');
getline(fin,性别[y],'\t');
getline(fin,score[y],'\t');
打破
}`
一旦我有了这些数组,我只需要找到一种按字母顺序排列的方法,但即使有了这些第一步,我还是被卡住了

编辑: 感谢您迄今为止的评论和帮助,我非常感谢您的时间。我的问题是,由于这是学校的项目工作,我需要使用数组(出于一些无法解释的原因)

仅供参考,在输入文件中,数字/姓名/性别/分数由制表符('/t')分隔

在坚持使用阵列而不使用矢量或地图的同时,有没有办法解决上述问题?

首先,使用:

for (int y=0 ; y<x, y++)
没有地图

struct row{string ID, name, rest;};

fout首先,使用:

for (int y=0 ; y<x, y++)
没有地图

struct row{string ID, name, rest;};


fout问题在于您试图读取int中的字符串:

getline(fin, UID [y], '\t');
Person* database = new Person[x];
UID[y]是一个int,而getline只能存储到字符串中

因此,您必须首先将其存储在缓冲区字符串中,然后使用例如:

string UID_buffer;
getline(fin, UID_buffer, '\t'); 
UID[y] = atoi(UID_buffer.c_str());
但是还有另一个问题,您的UID实际上太大,无法放入有符号int,您可以尝试将它们存储在无符号32或64位中,但将它们存储为字符串可能更简单

该方法建议您使用类来存储每个对象,而不是使用多个数组:

struct Person {
    string UID;
    string name;
    string gender;
    int score;
};
然后只创建一个数组

getline(fin, UID [y], '\t');
Person* database = new Person[x];

还可以创建一个C++可重容器,例如,它将更容易排序:

vector<Person> database;
使用如下定义的比较运算符:

for(int y = 0, y < x; y++) {

    getline(fin, line); 
    stringstream linestream(line);

    Person newPerson;

    getline(linestream, newPerson.UID, '\t'); 
    getline(linestream, newPerson.name, '\t'); 
    getline(linestream, newPerson.gender, '\t'); 

    string buffer;
    getline(linestream, buffer, '\t'); 
    newPerson.score = atoi(buffer.c_str());

    database.push_back(newPerson);
}
bool operator< (const Person & p1, const Person& p2)
{
    //just an example
    return p1.UID.compare(p2.UID) < 0;
}
bool运算符<(const Person和p1、const Person和p2)
{
//只是一个例子
返回p1.UID.compare(p2.UID)<0;
}
您可以了解有关重载运算符的更多信息

编辑

如果您不能使用vector,那么实际上不会更改循环代码(只需将newPerson替换为相应的已分配的Person对象(类似于数据库[x])

现在对于排序,您仍然可以使用STL的排序算法,它应该可以在迭代器上工作,但可以在指针上工作。这不是一个整洁的解决方案,可能不是您应该做的

否则,您可以实现自己的排序算法,这是一个非常好的解决方案


请注意,如果不使用结构并继续使用多个数组,则无法使用STL排序算法,并且排序算法中的数据交换将比必要时复杂得多。

问题在于,您试图读取int中的字符串。

getline(fin, UID [y], '\t');
Person* database = new Person[x];
UID[y]是一个int,而getline只能存储到字符串中

因此,您必须首先将其存储在缓冲区字符串中,然后使用例如:

string UID_buffer;
getline(fin, UID_buffer, '\t'); 
UID[y] = atoi(UID_buffer.c_str());
但是还有另一个问题,您的UID实际上太大,无法放入有符号int,您可以尝试将它们存储在无符号32或64位中,但将它们存储为字符串可能更简单

该方法建议您使用类来存储每个对象,而不是使用多个数组:

struct Person {
    string UID;
    string name;
    string gender;
    int score;
};
然后只创建一个数组

getline(fin, UID [y], '\t');
Person* database = new Person[x];

还可以创建一个C++可重容器,例如,它将更容易排序:

vector<Person> database;
使用如下定义的比较运算符:

for(int y = 0, y < x; y++) {

    getline(fin, line); 
    stringstream linestream(line);

    Person newPerson;

    getline(linestream, newPerson.UID, '\t'); 
    getline(linestream, newPerson.name, '\t'); 
    getline(linestream, newPerson.gender, '\t'); 

    string buffer;
    getline(linestream, buffer, '\t'); 
    newPerson.score = atoi(buffer.c_str());

    database.push_back(newPerson);
}
bool operator< (const Person & p1, const Person& p2)
{
    //just an example
    return p1.UID.compare(p2.UID) < 0;
}
bool运算符<(const Person和p1、const Person和p2)
{
//只是一个例子
返回p1.UID.compare(p2.UID)<0;
}
您可以了解有关重载运算符的更多信息

编辑

如果您不能使用vector,那么实际上不会更改循环代码(只需将newPerson替换为相应的已分配的Person对象(类似于数据库[x])

现在对于排序,您仍然可以使用STL的排序算法,它应该可以在迭代器上工作,但可以在指针上工作。这不是一个整洁的解决方案,可能不是您应该做的

否则,您可以实现自己的排序算法,这是一个非常好的解决方案


注意,如果你不使用一个结构并使用多个数组,那么你就不能使用STL排序算法,而且排序算法中的数据交换会比必要的复杂得多。

你到底在哪里粘贴?并且,在C++中,你可能想使用<代码> GETDelm < /Cord>,除非你真的需要T,否则你不使用数组。哼哼。您可以使用
std::vector
std::list
指令