Thread: Adapting this function

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    17

    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.
    Last edited by rhysmeister; 05-05-2004 at 04:28 PM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What is the reason for using a list container instead of, say, a vector?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    It's a legacy application running on an ancient dos machine. Can't really start messing around with the whole structure.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    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

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM