Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write data to disk file

Status
Not open for further replies.

bnd98

Technical User
Apr 1, 2003
17
US
I would like to write the following information to disk. How would I go about doing so? Any tips are greatly appreciated.

#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<<&quot; was written by &quot;<<name<<&quot;.&quot;<<endl;
cout<<&quot;Price: $ &quot;<<price<<&quot;. Total pages: &quot;<<pages<<endl;
cout<<&quot;&quot;<<endl;
};

void main()
{
Book books[10];
ofstream out(&quot;a:Book.txt&quot;);
if (out.fail())
cout<<&quot;File was not opened.&quot;;
else
books[0].setBook(&quot;Legacy&quot;, &quot;John Doe&quot;, 24.95, 185);
books[1].setBook(&quot;Disturbed&quot;, &quot;Jane Doe&quot;, 14.95, 75);
books[2].setBook(&quot;How To Love&quot;, &quot;Mark Davis&quot;, 39.95, 250);
books[3].setBook(&quot;Crafts for Kids&quot;, &quot;Jessica King&quot;, 19.95, 50);
books[4].setBook(&quot;Angry Faces&quot;, &quot;Melissa Hill&quot;, 9.95, 30);
books[5].setBook(&quot;World Explorer&quot;, &quot;Billy Joel&quot;, 59.95, 375);
books[6].setBook(&quot;Scared Of The Dark&quot;, &quot;B.J. Heckener&quot;, 29.95, 150);
books[7].setBook(&quot;Men That Can't Love&quot;, &quot;Helen Pat&quot;, 34.95, 200);
books[8].setBook(&quot;Pitbulls&quot;, &quot;Jack Frost&quot;, 14.95, 75);
books[9].setBook(&quot;Crazy&quot;, &quot;Little Jack&quot;, 19.95, 125);
for ( int i = 0; i < 10; i++)
books.displayBook();
out<<books;
getch();
};
 
You wrote:

[tt]out<<books;[/tt]

books is a pointer to Book, not a Book object itself.
What I think you need is

Code:
for (i=0; i<10; i++) {
   out<<books[i];
}

What I also think you need is an insertion operator for Book...

friend ostream& operator<<(ostream&, const Book&)

But what I really think you need is:

[tt]books[0].setBook(&quot;The C++ Programming Language&quot;,&quot;Bjarne Stroustrup&quot;,0,0);[/tt] :)
 
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:

#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<<&quot;Enter the title: &quot;;
cin>>title;
cin.get();
cout<<&quot;Enter the author: &quot;;
cin>>name;
cin.get();
cout<<&quot;Enter the price: $ &quot;;
cin>>price;
cin.get();
cout<<&quot;Enter amount of pages: &quot;;
cin>>pages;
cin.get();
cout<<endl;
};

void Book::displayBook()
{
cout<<title<<&quot; was written by &quot;<<name<<&quot;.&quot;<<endl;
cout<<&quot;Price: $ &quot;<<price<<&quot;. Total pages: &quot;<<pages<<endl;
cout<<endl;
};

ostream& write(ostream &out, const Book &aBook)
{
out<<aBook.title<<&quot; was written by &quot;<<aBook.name<<&quot;.&quot;<<endl;
out<<&quot;Price: $ &quot;<<aBook.price<<&quot;. Total pages: &quot;<<aBook.pages<<endl;
out<<endl;
return(out);
};

void main()
{
const numBooks = 10;
Book books[numBooks];
ofstream out(&quot;a:Book.txt&quot;);
for (int i = 0; i < numBooks; ++i)
{
books.enterBook();
out.write((char*)(&books), sizeof(books));
}
cout<<&quot;Data entry complete.&quot;<<endl;
getch();
};

 
>> It shows a lot of odd characters. What am I missing now?
>> Seems like something simple but I am unable to locate
>> the error.

Your missing the &quot;understanding&quot; part of &quot;how things work&quot;. Without the &quot;understanding&quot; everything is &quot;magic&quot; and that falls apart very quickly particularly with the C/C++ language.

>> Seems like something simple

There is that “simple” word again. This is a class assignment right? Who is telling you people that writing C/C++ software is “simple”? I would not categorize it as (simple), that is just not accurate.

The odd characters are what the data is represented as if you open the file in something like notepad. If you want to see the actual values of the data in the file open it with a hex editor or viewer.

For example, when you store a double value to disk it takes 4 bytes to store the value. A double is a complex type. It uses a data format within a 32 bit wide ( 4 bytes) memory space.

-pete



 
Actually it is not a class assignment, I am just trying to get a better understanding of the language. And I don't want to open it in a hex editor or viewer, I want to open it in notepad. And I know this is possible and I know that there is a way to do this but again I am missing something(obviously not simple since I am unable to figure it out). Thank you for your help.

 
>> I want to open it in notepad

Oh, well that's a significant piece of information we didn't have. Now i think i see what is happening.

>> out.write((char*)(&books), sizeof(books));

does not call:

>> ostream& write(ostream &out, const Book &aBook)

See, the signature does not match. Instead it does what I described in my previous post where a double gets written to a 4 byte value in the file. To view the double value 34.95 in the file using Notepad you need that function (ostream& write(ostream &out, const Book &aBook)) to write the data for you.

Since it takes a single Book object you need to do more like this in the for loop:

Code:
write(out, book[i]);

-pete



 
Pete,

Thank you very much for that information. It is working and saving to the file. Only one more problem that I would like to ask you about. When I open the file that it creates (book.data), it does not appear correctly for the title or the name. For instance, if I put the title as &quot;Crazy For C++&quot;, it cuts off &quot;Crazy&quot;. And it does the same for the name. Any suggestions for fixing that error?

Do you think the error would be in the enterData?
{
cout<<&quot;Enter the title: &quot;;
cin>>title;
cin.getline(title, 30);
cout<<&quot;Enter the author: &quot;;
cin>>name;
cin.getline(name, 30);
cout<<&quot;Enter the price: $ &quot;;
cin>>price;
cin.get();
cout<<&quot;Enter amount of pages: &quot;;
cin>>pages;
cin.get();
cout<<endl;
};

Or in the ostream and write?

{
out<<&quot;Title: &quot;<<aBook.title<<endl;
out<<&quot;Author: &quot;<<aBook.name<<endl;
out<<&quot;Price: $&quot;<<aBook.price<<endl;
out<<&quot;Total Pages: &quot;<<aBook.pages<<endl;
out<<endl;
return(out);
};

Also, if I use only the get function, instead of getline, if there is a space (Crazy C++), Crazy will be the title and C++ the author. Thought I would try it both ways, getline seems to be the best but again still not displaying the first word in multiple word entries.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top