Hello everyone, I am having a hard time figuring out how to form a circular doubly linked list from a text file. I know I have to store the data from the text file to a vector and then somehow store each vector element to the circular doubly linked list using the loop, but I am just not really sure how to do it and I cannot find any suitable example in the internet. Please help...
Here is the code I use, before doing any of these operations I want to form a circular doubly linked list with the data from the text file.
1. Determine the data you need in the file to populate your data structures. In this case, just space-separated integers.
2. Iterate over each piece of data in file and insert into your data structures. The <fstream> header makes things easy.
Vectors shouldn't be needed for this. Also, "start", "last", and "counter" should all likely be private members of your class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string file_name("my_file_name.txt");
ifstream ifs(file_name);
if (!ifs)
{
cout << "Error: unable to open '" << file_name << "'\n";
return -1;
}
int data;
while (ifs >> data)
{
// Create your node, populate "info", etc.
}
ifs.close(); // Optional. Closes file immediately, but it would also auto-close when out of scope
So by default it's looking in the run-time directory, usually the place where your executable is.
Either put a file named "1.txt" there, or in the code "give/it/the/full/path/to/1.txt".
On some IDEs you can also tweak the starting aka run-time aka working directory.
That's probably the vaguest thing in the world you could say.
Say instead, "I went to directory X, where I confirmed my.exe was there. Then I created a file 1.txt with a bunch of integers and placed it in that directory. Then I opened a console in that directory and ran my.exe".
Mention what you tried and how exactly you ran the compiled app.