-
Adapting this function
I've been messing around with a few new things. This function prints out entries from a binary file...
Code:
void file2list(string fname, BuckDoubleLinkedList<bookwithposition>& lst)
{
int index = 0;
bookobject bo;
ifstream is(fname.c_str(), ios::binary); //open binary input file
//while we can read a book from the file
while (is.read(reinterpret_cast<char *>(&bo), sizeof(bookobject)))
{
bookwithposition bwp(bo); //make book with position
bwp.setPosition(index++); //set position
lst.insert(bwp); //add to list
}
is.close(); //close file when we have done3
cout <<endl;
}
This function is called, the list is produced, and then the list is printed. Like so...
Code:
BuckDoubleLinkedList<bookwithposition> list('a');
file2list(fname, list);
list.printList(); //display list of contents
How could I adapt this function to print out, for example, the last 3, 5, 10 entries ect. Thanks.
-
What is the reason for using a list container instead of, say, a vector?
Kuphryn
-
It's a legacy application running on an ancient dos machine. Can't really start messing around with the whole structure.
-
A list container is no efficient for random access. One solution is iterator.
// Traverse to tenth element from the end of list container
std::const_iterator iRandom = listData.end() - 10;
Kuphryn
-
Just want to get this clear. I can use this with my current program, something like...
Code:
ifstream is(fname.c_str(), ios::binary);//open binary input file
std::const_iterator iRandom = lst.end() - 10; // set position
//while we can read a book from the file
while (is.read(reinterpret_cast<char *>(&bo), sizeof(bookobject)))
{
bookwithposition bwp(bo); //make book with position
bwp.setPosition(index++); //set position
lst.insert(bwp); //add to list
}
is.close();//close file when we have done
-
Think I have something that's almost there but when I call it it doesn't seem to output any of the list items.
Code:
void dolatest(string fname, BuckDoubleLinkedList<bookwithposition>& lst)
{
int index = 0; // declare and initialise index
bookobject bo;
ifstream is(fname.c_str(), ios::binary);//open binary input file
int count = 0; // cout used to discover the number of book entries
while (is.read(reinterpret_cast<char *>(&bo), sizeof(bookobject)))
{
bookwithposition bwp(bo); // make book with position
bwp.setPosition(count++); // set position
}
index = count - 5; // set the position to the last five entries
cout << index << endl;
//while we can read a book from the file
while (is.read(reinterpret_cast<char *>(&bo), sizeof(bookobject)))
{
bookwithposition bwp(bo); //make book with position
bwp.setPosition(index++); //set position
lst.insert(bwp); //add to list
}
is.close();//close file when we have done
cout << endl;
}
This is called by...
Code:
void latest(string fname)
{
BuckDoubleLinkedList<bookwithposition> list('a');
dolatest(fname, list); // call function to produce a list
list.printList(); // display the list
system("pause");
}
the dolatest function is adapted from a function which outputs all of the items which works fine. Any help appreciated.