how to right vector of pointers to binary file ?
-
how to right vector of pointers to binary file ? every pointer have diffrent size ,and i need to save to file and restore from file
it makes no sense to write a pointer to a file.
Asaf Shay wrote:
every pointer have diffrent size
pointers are always the same size (4 bytes in Win32, 8 bytes in Win64) are you trying to write the structures that the pointers point to to a file?
-
how to right vector of pointers to binary file ? every pointer have diffrent size ,and i need to save to file and restore from file
There are several things wrong here. First of, all pointers will be of the same size. On a 32-bit OS, all pointers are 4 bytes or 32-bits and on a 64-bit OS, all pointers are 8 bytes or 64-bits. Secondly, you do not want to persist pointers to a file, because they will not be valid when you read it back into the program as the address is not guaranteed to be the same every time the program is run. So what you need is to have a vector of objects/data type instead of a vector of pointers. This is the recommended method. Have a method of the object to return the data to be persisted. Typically this must be a structure. In the main program iterate through the elements of the vector, call the method that returns the data to be persisted and write it to a file. You can use any of the file operation routines like
CreateFile
/WriteFile
orfopen
/fwrite
to do this. You would also need a method of the object to put back the data when you read it from the file.«_Superman_» _I love work. It gives me something to do between weekends.
-
There are several things wrong here. First of, all pointers will be of the same size. On a 32-bit OS, all pointers are 4 bytes or 32-bits and on a 64-bit OS, all pointers are 8 bytes or 64-bits. Secondly, you do not want to persist pointers to a file, because they will not be valid when you read it back into the program as the address is not guaranteed to be the same every time the program is run. So what you need is to have a vector of objects/data type instead of a vector of pointers. This is the recommended method. Have a method of the object to return the data to be persisted. Typically this must be a structure. In the main program iterate through the elements of the vector, call the method that returns the data to be persisted and write it to a file. You can use any of the file operation routines like
CreateFile
/WriteFile
orfopen
/fwrite
to do this. You would also need a method of the object to put back the data when you read it from the file.«_Superman_» _I love work. It gives me something to do between weekends.
hi,tanks for the comments,my problem is i can write to file and read from it only if my program is running,if i close the program and open again and try to read from the file i get run time error "vector iterator not dereferencable"
case 'd'://save to file
{
file = fopen("final_proj.bin","w+b");
t = arr.begin();
fwrite( (*t), sizeof(figure3d), 1, file);
system("cls");
cout << "saved to file successfully\n"<case 'e'://restore from file
{
file = fopen("final_proj.bin" , "r+b" );
t = arr.begin();
fread ((*t), sizeof(figure3d) , 1 , file);
temp.push_back(*t);
temp[0]->print();
/*arr.push_back(*t);*/
fclose(file);
} -
hi,tanks for the comments,my problem is i can write to file and read from it only if my program is running,if i close the program and open again and try to read from the file i get run time error "vector iterator not dereferencable"
case 'd'://save to file
{
file = fopen("final_proj.bin","w+b");
t = arr.begin();
fwrite( (*t), sizeof(figure3d), 1, file);
system("cls");
cout << "saved to file successfully\n"<case 'e'://restore from file
{
file = fopen("final_proj.bin" , "r+b" );
t = arr.begin();
fread ((*t), sizeof(figure3d) , 1 , file);
temp.push_back(*t);
temp[0]->print();
/*arr.push_back(*t);*/
fclose(file);
}It could be because you're having a vector of pointers. Change that to vector of copyable objects.
«_Superman_» _I love work. It gives me something to do between weekends.