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