Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Binary file problem

Binary file problem

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    ganesh_IT
    wrote on last edited by
    #1

    Hi guys, i try to write my data into a binary file, but j have some problem in it . I dont know what happening inside..

    //////////////////////Here is my code/////////////////////////////
    class Person{
    char Name[50];
    char Address[10;
    bool sex;
    short age;
    public:
    Person();
    Person(char* name, char *address, bool sex, short age);
    const char* GetName() const;
    const char* GetAddress() const;
    };
    ostream& operator<<(ostream& out, const Person&p)
    {
    out.write(reinterpret_cast<const char*>(&ap), sizeof(p));
    return out;
    }
    istream& operator>>(istream& in, Person &p)
    {
    in.read(reinterpret_cast<char*>(&p), sizeof(p));
    return in;
    }
    //Person detail reading and writing is performed well

    //////////////////AccountProperty Class///////////////////////////////
    class Account_Property{
    int account_number;
    double amount;
    bool account_type;
    Person p;
    public:
    Account_Property();
    Account_Property(Person p, int ac_num, bool ac_type, double amt);
    int GetAccountNumber()const;
    double GetAmount()const;
    };
    ostream &operator<<(ostream &out, const Account_Property &ap)
    {
    out.write(reinterpret_cast<const char*>(&ap), sizeof(ap));
    return out;
    }
    istream& operator>>(istream &in, Account_Property &ap)
    {
    in.read(reinterpret_cast<char*>(&ap), sizeof(ap));
    return in;
    }
    //Account_Property readin writing is not performed perfectly ,whats the reason some one explain me.....

    Thanks in Advance

    N D 2 Replies Last reply
    0
    • G ganesh_IT

      Hi guys, i try to write my data into a binary file, but j have some problem in it . I dont know what happening inside..

      //////////////////////Here is my code/////////////////////////////
      class Person{
      char Name[50];
      char Address[10;
      bool sex;
      short age;
      public:
      Person();
      Person(char* name, char *address, bool sex, short age);
      const char* GetName() const;
      const char* GetAddress() const;
      };
      ostream& operator<<(ostream& out, const Person&p)
      {
      out.write(reinterpret_cast<const char*>(&ap), sizeof(p));
      return out;
      }
      istream& operator>>(istream& in, Person &p)
      {
      in.read(reinterpret_cast<char*>(&p), sizeof(p));
      return in;
      }
      //Person detail reading and writing is performed well

      //////////////////AccountProperty Class///////////////////////////////
      class Account_Property{
      int account_number;
      double amount;
      bool account_type;
      Person p;
      public:
      Account_Property();
      Account_Property(Person p, int ac_num, bool ac_type, double amt);
      int GetAccountNumber()const;
      double GetAmount()const;
      };
      ostream &operator<<(ostream &out, const Account_Property &ap)
      {
      out.write(reinterpret_cast<const char*>(&ap), sizeof(ap));
      return out;
      }
      istream& operator>>(istream &in, Account_Property &ap)
      {
      in.read(reinterpret_cast<char*>(&ap), sizeof(ap));
      return in;
      }
      //Account_Property readin writing is not performed perfectly ,whats the reason some one explain me.....

      Thanks in Advance

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      Your problem is with

      sizeof const Person&

      which should give the size of the reference and not the object. Change

      sizeof(p)

      to

      sizeof(Person)

      etc.

      home

      1 Reply Last reply
      0
      • G ganesh_IT

        Hi guys, i try to write my data into a binary file, but j have some problem in it . I dont know what happening inside..

        //////////////////////Here is my code/////////////////////////////
        class Person{
        char Name[50];
        char Address[10;
        bool sex;
        short age;
        public:
        Person();
        Person(char* name, char *address, bool sex, short age);
        const char* GetName() const;
        const char* GetAddress() const;
        };
        ostream& operator<<(ostream& out, const Person&p)
        {
        out.write(reinterpret_cast<const char*>(&ap), sizeof(p));
        return out;
        }
        istream& operator>>(istream& in, Person &p)
        {
        in.read(reinterpret_cast<char*>(&p), sizeof(p));
        return in;
        }
        //Person detail reading and writing is performed well

        //////////////////AccountProperty Class///////////////////////////////
        class Account_Property{
        int account_number;
        double amount;
        bool account_type;
        Person p;
        public:
        Account_Property();
        Account_Property(Person p, int ac_num, bool ac_type, double amt);
        int GetAccountNumber()const;
        double GetAmount()const;
        };
        ostream &operator<<(ostream &out, const Account_Property &ap)
        {
        out.write(reinterpret_cast<const char*>(&ap), sizeof(ap));
        return out;
        }
        istream& operator>>(istream &in, Account_Property &ap)
        {
        in.read(reinterpret_cast<char*>(&ap), sizeof(ap));
        return in;
        }
        //Account_Property readin writing is not performed perfectly ,whats the reason some one explain me.....

        Thanks in Advance

        D Offline
        D Offline
        Dave Kerr
        wrote on last edited by
        #3

        Can you edit your article text to show symbols rather than XML entities? I think Niklas is correct but it's rather hard to read!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups