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 to right vector of pointers to binary file ?

how to right vector of pointers to binary file ?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestion
5 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.
  • A Offline
    A Offline
    Asaf Shay
    wrote on last edited by
    #1

    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

    C _ 2 Replies Last reply
    0
    • A Asaf Shay

      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

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      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?

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • A Asaf Shay

        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

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        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 or fopen/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.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        A 1 Reply Last reply
        0
        • _ _Superman_

          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 or fopen/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.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          A Offline
          A Offline
          Asaf Shay
          wrote on last edited by
          #4

          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);
          }

          _ 1 Reply Last reply
          0
          • A Asaf Shay

            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);
            }

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            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.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            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