Hi

I want to know some information about (( files ))
I specified two questions and I hope to answer them.

First :
using function ( read and write )

fileName.write( reinterpret_cast< const char * >( & variableName ), sizeof( Ttype ) ) ;

in binary files only or can used in text files.

Second :
this code :

fileName.seekg( ( number - 1 ) * sizeof ( type) );

what is the appropriate type which I must put it in sizeof ( type)?
is it must be the same type of number ?
it is meaning : if the number from type int , the type in sizeof is int .
and if it function in class and it return type int , the type in sizeof is ... what ?
is int or the name of class ?

and this code is doing to moving the pointer with magnitude of number ?

.. I hope to clearing ..
.. thank you to help me..

1) It can be used in either text or binary, but most often used in binary files. Not very meaningful in text files.

2) The type]/b] is the object that is store in the file. For example, lets say you saved a bunsh of Personnel c++ classes, then it would be sizeof(Personnel) . If the file is binary file and contains integers, then sizeof(int) In your example, number is the quantity of Personnel records you want to search for. If you want to set the file stream pointer to the 10th Personnel record then number will be 10.

thank you for help me ..

you mean if I have class Personnel contains For example ID and name;
and I wanted to record (in binary file) in the appropriate position in the file according to the Personnel ID.
[ Meaning that in the file, Personnel should be stored in the file in order of Personnel ID. ]]
I will write :
fileName.seekg( ( ObjectOfPersonnel.getID() - 1 ) * sizeof ( Personnel) );
// to set in order of Personnel ID
fileName.write( reinterpret_cast< const char * >( & ObjectOfPersonnel ), sizeof( Personnel ) ) ;
// to write in file


is this right?

95% of the time this is wrong. The reasons are:

(a) classes with a virtual function you will get the wrong size (too big).

(b) classes that inherrit from a base class include the size of the base class but the pointer MIGHT NOT to point to the base class data area.

(c) If you have static objects they are not included (nor are they near the pointer in most representaions)

(d) If you have allocated memory (e.g. with a new) or memory that is allocated by a included object (e.g. std::vector) then you get the size of the pointer not the object.

So the way round it is normally to have the class write itself. (writeBinary is my normal method. I make it virtual)

Yes, I agree there are lots of potential problems and pitfalls serializing c++ classes. If the class contains pointers to other objects than serializing becomes much more complex and difficult.

>>fileName.seekg( ( ObjectOfPersonnel.getID() - 1 ) * sizeof ( Personnel) );

That MIGHT work if the ID numbers are sequential and start with 1.

StuXYZ
thank you for good information about size of the class in many of ways

Ok.
if I have not virtual function and no inheritance.
what the possible code to write my problem ?

I will write :

fileName.seekg( ( ObjectOfPersonnel.getID() + 1 ) * sizeof ( Personnel) );

// to set in order of Personnel ID .
// and +1 because the ID start of -1

fileName.write( reinterpret_cast< const char * >( & ObjectOfPersonnel ), sizeof( Personnel ) ) ;

// to write in file.


are you think that is true .?

>>are you think that is true
Maybe, and maybe not. Post the class. Does it contain std::string or char* ? If yes, than that won't work because the actual text is not part of the class, only a pointer or c++ class object (std::string).

Ancient Dragon

In may program , the ID start of -1 ;
(( the value from the teacher :( ))

so, I think the function serch from +1.

Ancient Dragon
sorry I didn't see your second Post.

My class contain only int and float ..
so Maybe is work .?

Ancient Dragon

In may program , the ID start of -1 ;
(( the value from the teacher :( ))

so, I think the function serch from +1.

But that should be incremented to 0 before writing to file. -1 is just a placeholder that says it has not been initialized yet.

My class contain only int and float ..
so Maybe is work .?

Most likely, yes. But I don't see how you could have a Personnel class without a name ??? Unless you are not working with a Personnel class but something else.

OK ..
and join name with ID in another a class.

Thank you very very very much
for help me.
I very happy now.


I will try this code in may programme.
and I think it work very good.

This article has been dead for over six months. Start a new discussion instead.