Thread: Editing a data file

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

    Editing a data file

    okay, I've been trying this all day and I can't get it to work for the life of me. I'm trying to fill an object from a data file then possibly make changes to it and put it back.

    the data file looks like this:

    Battlefield Earth //title of a book
    L. Ron Hubbard //author of the book
    3 //copies of the book in the database
    15.95 //price of the book

    here's the class headers:
    Code:
    class Book
    {
      public:
        void operator = (const Book& b1);
        Book();              //sets title and author to empty strings
                                 //copies to 0 and price to 0.0
        string get_title(); //returns title
        string get_author(); //returns author
        void set_title(string str); //changes value of title
        void set_author(string str); //changes value of author
        void set_copies(int num); //changes value of copies
        void set_price(double p); //changes value of price
        int get_copies(); //returns copies
        double get_price(); //returns price
        void print(ostream& outs); //outputs all the data in the format 
                                                   //I showed above
        void fill(istream& ins); //fills the values
      private:
        string title;
        string author;
        int copies;
        double price;
    };
    
    //here's the second class
    
    typedef Book* Bookptr;
    
    class Library
    {
      public:
        ~Library();
        Library(const Library& l1);
        void operator = (const Library& l1);
        Library(); //sets size to 1 and allocates memory for books
        void search (); //searches by author or title
        void  print(ostream& outs); //prints books in format above
        void add_new(const string& s1, fstream& file); //adds a new 
    //book to the library or increments the number of copies if it is
    //already in the database. this is the function I've been having 
    //trouble with
        void fill(istream& ins, int& num); //input function
        void total(); //adds up price of all the books and prints it
        void delete_book(const string& s1); //deletes a book from the 
    //library, another one I'm having trouble with
        void check_out(const string& s1); //checks a book out
        void random(); //prints a random book
      private:
        int size;
        Bookptr books;
        void resize(Bookptr& b1, int& num);
    };
    
    //all of these functions work really well, the only problem I'm 
    //having is that my data file never gets permanently edited even 
    //when I want to. here's the definitions of the functions that 
    //matter
    
    void add_new(const string& s1, fstream& file)
    {
      Book b1;
      string str;
      int temp;
      double temp_price;
    
      for (int i = 0; i < size; i++)
      {
        if (new_title == books[i].get_title())
        {
          temp = books[i].get_copies() + 1;
          books[i].set_copies(temp);
         }
         return;
      }
    //if the book isn't in the database already, the rest of this 
    //function gets its info from the keyboard, puts it into the books 
    //array, and prints it into the data file. that works fine, the part 
    //above is what I need help with. I need to know how to scrap 
    //the old value of copies and print the new one out to the same 
    //place. I was thinking of something like this:
    
      file.open("database.dat",ios::out|ios::trunc);
      print(file);
      file.close();
    
      //but every time I try that it just deletes the data file and 
      //doesn't fill it again
    ultimately what I'm going to need to be able to is open the file and fill a Library object from it. then I need to be able to edit it. and I need to be able to change individual lines of it. I can't figure out how for the life of me. the easiest way I think would be to delete the file then reprint the new data

    if you need more code I can provide it, but I can't think of any that you'd really need. none of the other functions are called in this piece of code

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    How is print defined?
    Kampai!

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    Code:
    void Book::print(ostream& outs)
    {
      outs << title << endl << author << endl << copies << endl << price << endl;
    }
    
    void Library::print(ostream& outs)
    {
      for (int i = 0; i < size; i++)
        books[i].print(outs);
    }
    
    //sorry, I meant to include that in the original post, but I forgot

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is this a text file? Or a binary file with fixed length records?

    If the former, you can't simply edit one and place it back in the middle. You have to reconstruct the entire file again from that record onward.

    If the latter, you can simply seek to the position of the record and write it back to that spot (assuming you've opened the file correctly).

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    yeah it's a text file. I'm not very familiar with binary files

    so back to my original point, how to I rewrite the file once I've inputted from it?

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You should try taking a look at the FAQ

    The C++ answer is also in the FAQ

    After reading those if you have questions feel free to ask.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    there's nothing in there that has anything to do with my problem except I learned that ios::trunc doesn't really delete, it just sets file length to 0. I think that explains why my other idea wasn't working

    but, I still have no idea how to do this

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    That's funny because when I do the below code it erases whatever I have in the file and then replaces it with the letter z.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    
    int main() {
    
    	fstream fout;
    
    	fout.open("mytext.txt", ios_base::out | ios_base::trunc);
    
    	if(fout.is_open()) {
    		fout.put('z');
    	}else {
    		cout <<"error opening file";
    	}
    
    	fout.close();
    	return 0;
    }
    Are you sure you were using the right combinations for the writing flags?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM