Thread: Write data to a disk file

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Write data to a disk file

    In the following code I would like to write the information to a disk. However I have been unable to figure this out. What do I need to include to make this possible? I have it successfully creating the new file, but it is not adding the correct information. Any tips are greatly appreciated.

    Code:
    #include<fstream.h>
    #include<conio.h>
    #include<string.h>
    
    class Book
    {
    private:
    	char title[30];
    	char name[30];
    	double price;
    	int pages;
    public:
    	void setBook(char t[], char n[], double pr, int pa);
    	void displayBook();
    };
    
    void Book::setBook(char t[], char n[], double pr, int pa)
    {
    	strcpy(title, t);
    	strcpy(name, n);
    	price = pr;
    	pages = pa;
    };
    
    void Book::displayBook()
    {
    	cout<<title<<" was written by "<<name<<"."<<endl;
    	cout<<"Price: $ "<<price<<". Total pages: "<<pages<<endl;
    	cout<<""<<endl;
    };
    
    void main()
    {
    	Book books[10];
    	ofstream out("a:Book.txt"); 
    	if (out.fail())
    		cout<<"File was not opened.";
    	else
    	books[0].setBook("Legacy", "John Doe", 24.95, 185);
    	books[1].setBook("Disturbed", "Jane Doe", 14.95, 75);
    	books[2].setBook("How To Love", "Mark Davis", 39.95, 250);
    	books[3].setBook("Crafts for Kids", "Jessica King", 19.95, 50);
    	books[4].setBook("Angry Faces", "Melissa Hill", 9.95, 30);
    	books[5].setBook("World Explorer", "Billy Joel", 59.95, 375);
    	books[6].setBook("Scared Of The Dark", "B.J. Heckener", 29.95, 150);
    	books[7].setBook("Men That Can't Love", "Helen Pat", 34.95, 200);
    	books[8].setBook("Pitbulls", "Jack Frost", 14.95, 75);
    	books[9].setBook("Crazy", "Little Jack", 19.95, 125);
    	for ( int i = 0; i < 10; i++)
    		books[i].displayBook();
    	out<<books;
    	getch();
    };

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll need to open your file correctly so it doesn't truncate the file every time you open it. You'll also need to close the file once you're done writing. If you don't, there's no guarintee (AFAIK) that your data will get written.

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

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Question

    I understand what you are saying, at least the reasons for doing so. However I am unsure of how I would go about doing this. Any additional suggestions that may be able to clear this up for me?

  4. #4
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28
    Code:
    main()
    {
     ofstream out;
     out.open(filename, ios::nocreate | iso::out);
     ...
     ...
     ...
     out.close();
     ...
     return 0;
    }
    That should do it

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>main()

    That's only legal in C. C++ has to be int main( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by XSquared
    >>main()

    That's only legal in C. C++ has to be int main( ).
    also to mention, bnd98's original code had void main(). not legal in c or c++.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    I have changed a lot in my code. The problem I have now is that when it does write the information to disk, it does not write it correctly. It shows a lot of odd characters. What am I missing now? Seems like something simple but I am unable to locate the error.

    Now I would like for the user to enter the information, then the information sent to the file. This is what I have:

    Code:
    #include<fstream.h>
    #include<conio.h>
    #include<string.h>
    
    class Book
    {
    	friend ostream& write(ostream &out, const Book &aBook);
    private:
    	char title[30];
    	char name[30];
    	double price;
    	int pages;
    public:
    	void enterBook();
    	void displayBook();
    };
    
    void Book::enterBook()
    {
    	cout<<"Enter the title: ";
    	cin>>title;
    	cin.get();
    	cout<<"Enter the author: ";
    	cin>>name;
    	cin.get();
    	cout<<"Enter the price: $ ";
    	cin>>price;
    	cin.get();
    	cout<<"Enter amount of pages: ";
    	cin>>pages;
    	cin.get();
    	cout<<endl;
    };
    
    void Book::displayBook()
    {
    	cout<<title<<" was written by "<<name<<"."<<endl;
    	cout<<"Price: $ "<<price<<". Total pages: "<<pages<<endl;
    	cout<<endl;
    };
    
    ostream& write(ostream &out, const Book &aBook)
    {
    	out<<aBook.title<<" was written by "<<aBook.name<<"."<<endl;
    	out<<"Price: $ "<<aBook.price<<". Total pages: "<<aBook.pages<<endl;
    	out<<endl;
    	return(out);
    };
    
    void main()
    {
    	const numBooks = 10;
    	Book books[numBooks];
    	ofstream out("a:Book.txt"); 
    	for (int i = 0; i < numBooks; ++i)
    	{
    		books[i].enterBook();
    		out.write((char*)(&books[i]), sizeof(books[i]));
    	}
    	cout<<"Data entry complete."<<endl;
    	getch();
    };
    Can anyone see the error(s) in my code that I just can not find?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    ostream& write(ostream &out, const Book &aBook)

    the above wants an ostream as the first parameter and a constant reference to a Book as the second whereas the following is sending a char * and a long. Also since write() is the name of an ostream method that uses the parameters as below then the compiler is probably calling that up so it isn't calling it a bug.

    out.write((char*)(&books[i]), sizeof(books[i]));

    I would rename the write member function to avoid confusion. I would only send the ostream as a parameter. I would call the member function using the object at hand, similar to what you did with enterBook().

  9. #9
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Originally posted by alpha
    also to mention, bnd98's original code had void main(). not legal in c or c++.
    That is to say... it's not legal in ALL versions of C/C++. But it's probably legal in his version. I know it is with Borland.

    I'm not saying it's a good practice though...

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Frobozz
    That is to say... it's not legal in ALL versions of C/C++. But it's probably legal in his version. I know it is with Borland.

    I'm not saying it's a good practice though...
    Just because it works doesn't mean it's right.

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    Thank you all very much. I finally did get it to work thanks to your help. Now all I want to do is get it to read into the file correctly!

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Great, now use the new headers:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. write data to end of file
    By sujeet1 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2007, 12:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM