Thread: I/O headache

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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