Thread: I/O headache

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    46

    I/O headache

    okay, I'm trying to input a bunch of data from a file then print it onto the screen. it's a little too complicated to explain so here's the code:

    Code:
    //first off, here's what the data file looks like
    
    Title of a Book
    First Last               //author's name
    X                            //number of copies
    XX.XX                     //price
    
    //there's two classes, one holds all the data for a book, the other
    // holds a dynamic array of books
    
    class Book
    {
      public:
        //bunch of accessor and set functions, only these two matter
        void print(ostream& outs);
        void fill(istream& ins);
      private:
        string title, author;
        int copies;
        double price;
    };
    
    typedef Book* Bookptr;
    
    class Library
    {
      public:
        //big three, accessor functions, sort, search, etc only these       
        //matter
        void print(ostream& outs);
        void fill(istream& ins, int& num);
      private:
        int size;
        Bookptr books;
        void resize(Bookptr b1, int& num);
    };
    
    //here's the implementation for the ones that matter:
    
    void Book::print(ostream& outs)
    {
      outs << title << endl << author << endl << copies << endl
              << price << endl;
    }
    void Book::fill(istream& ins)
    {
      getline(ins,title);
      getline(ins,author);
      ins >> copies;
      ins >> price;
    }
    
    void Library::print(ostream& outs)
    {
      for (int i = 0; i < size; i++)
        books[i].print(outs);
    }
    
    void Library::fill(istream& ins, int& num)
    {
      books[num].fill(ins);
      if (num == (size - 1))
        resize(books,size);
    }
    
    Library::Library()
    {
      size = 1;
      books = new Book[size];
    }
    
    void Library::resize(Bookptr& b1, int& num)
    {
      Bookptr temp;
    
      temp = new Book[size + 1]
      for (int i=0; i < size; i++)
        temp[i] = b1[i];     //assignment operator is overloaded
      delete [] b1;
      b1 = temp;
      size++;
    }
      
    //basically I'm just testing these for now, the library fill function
    //will eventually have a loop in it, but for now I need it to work
    // here's what I'm doing in my main file
    
    int main()
    {
      ifstream fin;
      Library l1;  //default constructor sets size = 1
      int i = 0;
    
      fin.open("database.dat");
    
      l1.fill(fin, i);  //I'll eventually use a loop, but this is for testing
      i++;
      l1.fill(fin,i);
    
      fin.close();
    
      l1.print(cout);
     
      return 0;
    }
    it's in five files. book class header and implementation, library class header and implementation, and the main file.

    I get no syntax or run-time errors, and the first time I try call fill it works perfectly and prints it perfectly. but the second time it gets the title and then nothing after that. see if you can find anything wrong with this. I'll keep trying in the mean time
    Last edited by Strait; 02-04-2005 at 09:57 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Hard to say when you miss out so much functionality from your post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Need more code, especially your classes. You mention a default constructor in your comment, "default constructor sets size = 1" but you don't include that code. Also you seem to have a resize member function that I can guess resizes your dynamically allocated memory but I don't see any code for that either. You are entering two items into your Library class but you do not call this resize function even though your comment says the constructor only allocates memory for 1 object. Bottom line: we are missing the whole picture with the limited amount of code you are showing.

    Also, could you simply use an STL container for this program instead of the Library class? You wouldn't need to worry about the memory management aspects if you did use one.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    I accidently left the resize function out, and I think my default constructor is fine, but it's up there now

    if you need any more code let me know, I just don't want to post up more than you really need

    also, I don't know what an STL container is. this is an assignment for a class and we're supposed to be learning to use pointers and such things

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't know what your input file looks like, but assuming each title start on a new line, you probably need an ignore in your fill() method to eat the newline. This is where some cout's would help, to be sure everythings happening correctly.
    Code:
    void Book::fill(istream& ins)
    {
      getline(ins,title);
      getline(ins,author);
      ins >> copies;
      ins >> price;
      ins.ignore(std::numeric_limits < int >::max(), '\n');
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    sweet, it's working now. thanks, I completely forgot to put ignore in there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw I/O vs. Stream I/O
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 08:32 AM
  2. Buffered/UnBuffered I/O
    By valaris in forum C Programming
    Replies: 1
    Last Post: 08-06-2008, 11:31 PM
  3. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  4. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM