Results 1 to 6 of 6

Thread: Read Binary value

  1. #1

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Exclamation 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

  2. #2
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    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;
    }

  3. #3

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    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
    Attached Images Attached Images  

  4. #4
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: Read Binary value

    it works for me. are you sure you deleted test.dat before you ran the new writing program?

  5. #5

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Re: Read Binary value

    If i delete test.dat, how can i specify a file name where to write them?

  6. #6

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    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
  •  



Click Here to Expand Forum to Full Width