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. How can I Move a memory pointer in FILE?

How can I Move a memory pointer in FILE?

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
6 Posts 6 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.
  • 0 Offline
    0 Offline
    002comp
    wrote on last edited by
    #1

    Hello friends I am writing in a Binary File Using FILE object. And the structure that I am using while Writing is : class A { int a; int b; }; While Reading Mine structure is : class B { string name; int a; int b; } I am using fread like this way: B* obj = new B(); fread(obj,sizeof(B)-sizeof(string),1,stream); but not working,I m assuming that memory pointer obj is pointing at string name,so thats why i want to move obj pointer to int a,so it will read value of int a. If ,i am wrong,please sugget me some other way. But there is restriction that i can change struture. Thanks In Advance. Regards Yogesh

    C M enhzflepE L 4 Replies Last reply
    0
    • 0 002comp

      Hello friends I am writing in a Binary File Using FILE object. And the structure that I am using while Writing is : class A { int a; int b; }; While Reading Mine structure is : class B { string name; int a; int b; } I am using fread like this way: B* obj = new B(); fread(obj,sizeof(B)-sizeof(string),1,stream); but not working,I m assuming that memory pointer obj is pointing at string name,so thats why i want to move obj pointer to int a,so it will read value of int a. If ,i am wrong,please sugget me some other way. But there is restriction that i can change struture. Thanks In Advance. Regards Yogesh

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      try

      &obj->a

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<

      1 Reply Last reply
      0
      • 0 002comp

        Hello friends I am writing in a Binary File Using FILE object. And the structure that I am using while Writing is : class A { int a; int b; }; While Reading Mine structure is : class B { string name; int a; int b; } I am using fread like this way: B* obj = new B(); fread(obj,sizeof(B)-sizeof(string),1,stream); but not working,I m assuming that memory pointer obj is pointing at string name,so thats why i want to move obj pointer to int a,so it will read value of int a. If ,i am wrong,please sugget me some other way. But there is restriction that i can change struture. Thanks In Advance. Regards Yogesh

        M Offline
        M Offline
        Madhu Nair 0
        wrote on last edited by
        #3

        Try ofstream[^] for serializing the objects in C++ way. Here[^] is an example. Check this[^] site if you are interested to learn how to serialize in MFC.

        1 Reply Last reply
        0
        • 0 002comp

          Hello friends I am writing in a Binary File Using FILE object. And the structure that I am using while Writing is : class A { int a; int b; }; While Reading Mine structure is : class B { string name; int a; int b; } I am using fread like this way: B* obj = new B(); fread(obj,sizeof(B)-sizeof(string),1,stream); but not working,I m assuming that memory pointer obj is pointing at string name,so thats why i want to move obj pointer to int a,so it will read value of int a. If ,i am wrong,please sugget me some other way. But there is restriction that i can change struture. Thanks In Advance. Regards Yogesh

          enhzflepE Offline
          enhzflepE Offline
          enhzflep
          wrote on last edited by
          #4

          I'd be inclined to load them individually. I.e

          B *obj = new B();
          fread(&obj->a, sizeof(obj->a), 1, stream);
          fread(&obj->b, sizeof(obj->b), 1, stream);

          Please note also: the compiler may pad your structures where it sees fit, unless prevented via a compiler directive. I'd assume it also applied for classes, though do not know. The implication is that if you declare a struct thusly:

          struct simpleStruct{
          char memberA;
          long memberB;
          }

          You can often find that doing sizeof on each of the members then adding them together will give you a different result to sizeof theWholeStructure. I.e, in my above example, it's quite possible that - sizeof(char) + sizeof(long) != sizeof(simpleStruct). You may find that when you expect your structure to be 5 bytes as in the above example, that the compiler will pad it to 6 or 8 bytes. This means that when you do an fwrite using &simpleStuctInstance, you may in fact be adding 6 or 8 bytes to the file, although there are only 5 bytes of useful information. If you then do a fread using &simpleStructInstance, the extra bytes of padding will be magically of no consequence. However, if you fwrite the individual members out individually, then go onto fread the whole structure in one go, you'll be reading 6 or 8 bytes, discarding 1 or 3 of them and ending up with a struct that contains garbage. This issue often arises when writing roll-your-own code for bitmap handling - failing to direct the compiler not to pad the structs results in all sorts of nasty behaviour.

          C 1 Reply Last reply
          0
          • enhzflepE enhzflep

            I'd be inclined to load them individually. I.e

            B *obj = new B();
            fread(&obj->a, sizeof(obj->a), 1, stream);
            fread(&obj->b, sizeof(obj->b), 1, stream);

            Please note also: the compiler may pad your structures where it sees fit, unless prevented via a compiler directive. I'd assume it also applied for classes, though do not know. The implication is that if you declare a struct thusly:

            struct simpleStruct{
            char memberA;
            long memberB;
            }

            You can often find that doing sizeof on each of the members then adding them together will give you a different result to sizeof theWholeStructure. I.e, in my above example, it's quite possible that - sizeof(char) + sizeof(long) != sizeof(simpleStruct). You may find that when you expect your structure to be 5 bytes as in the above example, that the compiler will pad it to 6 or 8 bytes. This means that when you do an fwrite using &simpleStuctInstance, you may in fact be adding 6 or 8 bytes to the file, although there are only 5 bytes of useful information. If you then do a fread using &simpleStructInstance, the extra bytes of padding will be magically of no consequence. However, if you fwrite the individual members out individually, then go onto fread the whole structure in one go, you'll be reading 6 or 8 bytes, discarding 1 or 3 of them and ending up with a struct that contains garbage. This issue often arises when writing roll-your-own code for bitmap handling - failing to direct the compiler not to pad the structs results in all sorts of nasty behaviour.

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            That is really good advice concerning padding. It more often than not will cause huge grief and head aches when you ignore the effect of it. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            1 Reply Last reply
            0
            • 0 002comp

              Hello friends I am writing in a Binary File Using FILE object. And the structure that I am using while Writing is : class A { int a; int b; }; While Reading Mine structure is : class B { string name; int a; int b; } I am using fread like this way: B* obj = new B(); fread(obj,sizeof(B)-sizeof(string),1,stream); but not working,I m assuming that memory pointer obj is pointing at string name,so thats why i want to move obj pointer to int a,so it will read value of int a. If ,i am wrong,please sugget me some other way. But there is restriction that i can change struture. Thanks In Advance. Regards Yogesh

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              This is so wrong! If you go with this and release it into your commercial application then some developer some day is bound to change one of the classes but forget about the other one, at which time you system comes to a grinding halt. You should never use one class to save your data and a different one to restore it. Get your design right at the beginning and it will pay dividends in the future.

              Unrequited desire is character building. OriginalGriff

              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