|
-
Jan 15th, 2006, 08:57 PM
#1
Thread Starter
Hyperactive Member
Read Binary value
Hi there,
Code:
#include <iostream>
#include <fstream>
using std::cin;
using std::cout;
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::ios;
using std::endl;
struct Books
{
char *BookName;
int Pages;
};
void write_binary(Books A);
void read_binary();
int main()
{
Books A;
A.BookName="C++";
A.Pages=450;
write_binary(A);
return 0;
}
void write_binary(Books A)
{
fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
binary_file.write(reinterpret_cast<char *>(&A),sizeof(Books));
binary_file.close();
}
void read_binary()
{
Books B;
fstream binary_file("c:\\test.dat",ios::binary|ios::in);
binary_file.read(reinterpret_cast<char *>(&B),sizeof(Books));
binary_file.close();
cout<<"Book's name : "<<B.BookName<<endl;
cout<<"Pages :"<< B.Pages<<endl;
}
Code:
int main()
{
read_binary();
return 0;
}
the result i have seen is
Book's name : C:\test.dat
Pages : 1
Why the result is not the same as i have assigned?
Thanks for advance
-
Jan 16th, 2006, 03:30 AM
#2
Frenzied Member
Re: Read Binary value
the problem is that you're merely writing / reading the value of the char pointer, not the string
delete test.dat, then try these changes for the input
Code:
struct Books
{
char BookName[30];
int Pages;
};
void write_binary(Books A);
void read_binary();
int main()
{
Books A;
strcpy(A.BookName, "C++");
A.Pages=450;
write_binary(A);
return 0;
}
-
Jan 17th, 2006, 08:35 PM
#3
Thread Starter
Hyperactive Member
Re: Read Binary value
The result of this time is in the attach file
please see and help me how should i solve it.
do you have any example for writing/reading binary file?
thanks
-
Jan 17th, 2006, 09:43 PM
#4
Frenzied Member
Re: Read Binary value
it works for me. are you sure you deleted test.dat before you ran the new writing program?
-
Jan 17th, 2006, 10:26 PM
#5
Thread Starter
Hyperactive Member
Re: Read Binary value
If i delete test.dat, how can i specify a file name where to write them?
-
Jan 17th, 2006, 10:36 PM
#6
Thread Starter
Hyperactive Member
Re: Read Binary value
Hey dis1411, It just works for me.
maybe i was mistake something.
can you help me a little more....
If i have inserted for 3 records, how can i move to next one?
Thanks a lot dis1411
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|