[英]C++ Reading a file line by line with delimiter and store data into a list
I have a class called Task that have 我有一堂课叫做Task
//Attributes
int earliestStartingTime, earliestEndTime, latestStartingTime, latestEndTime, duration, differ;
char TaskPosition;
list<Task*> predecessors;
list<Task*> successors;
//Constructor
Task::Task(char TaskPosition2, int duration, list<Task*> predecessors)
{
this-> TaskPosition = TaskPosition2;
this -> duration = duration;
for(list<Task*>::iterator it = this-> predecessors(); it != this->predecessors.end(); it++)
{
this-> predecessors.push_back(*it);
}
}
I am trying to read a file line by line and store the data into the list task. 我正在尝试逐行读取文件并将数据存储到列表任务中。
In the file, the data are store as this 在文件中,数据按此存储
A(3) A(3)
B(4),A B(4),A
C(2),A C(2),A
E(5),A E(5),A
G(3),A G(3),A
J(8),B,H J(8),B,H
H(7),C,E,G H(7),C,E,G
I(6),G I(6),G
F(5),H F(5),高
M(4),I ... M(4),我...
And I want to be able to add them in the list of Task as the following 我希望能够将它们添加到“任务”列表中,如下所示
Task(char TaskPosition, int duration, list predecessors) so for example: Task(char TaskPosition,int持续时间,列出前任),例如:
A is the task position A是任务位置
3 is the duration 持续时间是3
No predecessors 没有前任
B is the task position B是任务位置
4 is the duration 持续时间是4
A is the predecessor A是前身
... J is the task position ... J是任务位置
8 is the duration 持续时间是8
B,H are the predecessors of J B,H是J的前身
and so on. 等等。
I will apply some calculation later to that list such as, what's the shortest way to get from A to Z etc.... 稍后我将对该列表进行一些计算,例如从A到Z等的最短方法是什么。
But I am still stuck at reading the file and adding them to the list. 但是我仍然停留在读取文件并将其添加到列表中。
This is what I have so far. 到目前为止,这就是我所拥有的。
int _tmain(int argc, _TCHAR* argv[])
{
string line;
list< Task>* allTask = new list<Task>;
const string file = "Test01.txt"; // text file
ifstream file;
if(file.is_open())
{
while(getline(file, line, ','))
{
allTask.push_back(line);
}
}
}
This line 这条线
allTask.push_back(line);
is giving me an error message that 给我一个错误信息
the lest side of .push_back must have class/struct/union
So I am not sure anymore what to do. 所以我不知道该怎么办。 Anyone can please help me figure out how to read the file add the data into the list please.
任何人都可以帮助我弄清楚如何读取文件,并将数据添加到列表中。
Thank you 谢谢
allTask
is a pointer so you have to use ->
to use member function directly. allTask
是一个指针,因此您必须使用->
直接使用成员函数。 Instead of changing that though there is no reason to have allTask
as a pointer. 尽管没有理由将
allTask
用作指针,但无需更改它。 You can simply use 您可以简单地使用
list< Task> allTask;
And now allTask
is an automatic object and you can use .
现在
allTask
是一个自动对象,您可以使用.
to call it's member functions. 调用它的成员函数。
You might do well reading up and possibly using a serialization deserialization library. 您可能会很好地阅读并可能使用序列化反序列化库。 This will do exactly what you require and it will give you insight in how to do it in a conventional way.
这将完全满足您的要求,并让您深入了解如何以常规方式进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.