Hello everyone, I am having a hard time figuring out how to form a circular doubly linked list from a text file. With this code I manage to read the start node and the last node from the text file, but I am not sure how to read all the data to the list properly and in a rational way as possible. What should I add to my code?
The only place I could find where you read your file is on line 38. You would need to read the file inside the while loop if you want to use any data from the file. Right now the value of "value" does not change from the first read.
The header file <cstdio> is not necessary because <iostream> covers every thing you need in this code.
Thanks, but when I do this, it always prints: error, unable to open the file :(
The relevant snippet, which you had in there, was correct:
1 2 3 4 5 6 7 8 9 10 11 12
ifstream fin("1.txt");
if (!fin)
{
cout << "Error: unable to open " << "1.txt" << endl;
return -1;
}
int info;
while (fin >> info)
{
cdl.create_node(info);
}
fin.close();
That line is there on purpose to protect your loop from reading garbage. If the file can't be read, the program should stop, or bad things could happen. I see you've removed the safety measures. You're not solving the problem, which I tried to emphasize in my last posts of the other thread -- put the file in the runtime directory!
You're not using classes correctly. You shouldn't have any global variables. start, last and counter should be member variables of double_clist. And node should have a constructor.
Also, in C++:
* Don't use stdio (take it out of your includes)
* Don't use NULL. Pre-C++11, use 0, otherwise use the modern nullptr.
* Don't say struct before the struct name to use a struct (only to define it).
* Try to declare variables close to their first use.
And try to initialize them at their declaration.
And declare loop indices inside the loop header.
* Use new (not malloc) and delete (not free).