-
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? :confused:
Thanks for advance
-
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;
}
-
1 Attachment(s)
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
-
Re: Read Binary value
it works for me. are you sure you deleted test.dat before you ran the new writing program?
-
Re: Read Binary value
If i delete test.dat, how can i specify a file name where to write them?
-
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 :)