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. C++ File system

C++ File system

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helplearning
5 Posts 2 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.
  • H Offline
    H Offline
    hrishiS
    wrote on last edited by
    #1

    Hi to All, I have 4 rows of data has to be saved in a binary file system . I haven't use it before, Can anyone please help me with this EG: struct A { int a1; int a2; } struct B { int b1; int b2; } struct C { int c1; int c2; } How do I write and read this values from the file system??

    ----------------------------- I am a beginner

    CPalliniC 1 Reply Last reply
    0
    • H hrishiS

      Hi to All, I have 4 rows of data has to be saved in a binary file system . I haven't use it before, Can anyone please help me with this EG: struct A { int a1; int a2; } struct B { int b1; int b2; } struct C { int c1; int c2; } How do I write and read this values from the file system??

      ----------------------------- I am a beginner

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You may simply store the structs as row, binary data. For instance (error checking left to the reader):

      struct A
      {
      int a1;
      int a2;
      };

      A a;

      FILE * fp = fopen("data.raw", "wb");

      fwrite(&a, sizeof(a), 1, fp);
      //..
      fclose(fp);
      //..

      For an object oriented approach see Object Serialization [^](for instance MFC framework supports it). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      H 1 Reply Last reply
      0
      • CPalliniC CPallini

        You may simply store the structs as row, binary data. For instance (error checking left to the reader):

        struct A
        {
        int a1;
        int a2;
        };

        A a;

        FILE * fp = fopen("data.raw", "wb");

        fwrite(&a, sizeof(a), 1, fp);
        //..
        fclose(fp);
        //..

        For an object oriented approach see Object Serialization [^](for instance MFC framework supports it). :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        H Offline
        H Offline
        hrishiS
        wrote on last edited by
        #3

        thanks for your reply. But my problem is I have more then 2 structure and I need to search or write at runtime.

        ----------------------------- I am a beginner

        CPalliniC 1 Reply Last reply
        0
        • H hrishiS

          thanks for your reply. But my problem is I have more then 2 structure and I need to search or write at runtime.

          ----------------------------- I am a beginner

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          You may do a step toward serialization, storing type info just before actual data, for instance:

          enum SER_TYPES
          {
          eTypeI,
          eTypeD,
          //..
          };

          struct I
          {
          int i1, i2;
          };
          struct D
          {
          double d1, d2, d3;
          };
          // ..

          void store(FILE *fp, I * pi)
          {
          int t = eTypeI;
          fwrite( &t, sizeof(int), 1, fp); // first write type
          fwrite( pi, sizeof(*pi), fp); // then write data
          }
          void store(FILE *fp, D * pd)
          {
          int t = eTypeD;
          fwrite( &t, sizeof(int), 1, fp); // first write type
          fwrite( pd, sizeof(*pd), fp); // then write data
          }

          int main()
          {
          FILE * fp = fopen("data.raw", "wb");
          I i,j;
          D d,f;
          //..
          store(fp,i);
          store(fp,d);
          store(fp,j);
          //..
          }

          On reading, you've to first read the type of the stored data and then, according to the type, read the actual data. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          H 1 Reply Last reply
          0
          • CPalliniC CPallini

            You may do a step toward serialization, storing type info just before actual data, for instance:

            enum SER_TYPES
            {
            eTypeI,
            eTypeD,
            //..
            };

            struct I
            {
            int i1, i2;
            };
            struct D
            {
            double d1, d2, d3;
            };
            // ..

            void store(FILE *fp, I * pi)
            {
            int t = eTypeI;
            fwrite( &t, sizeof(int), 1, fp); // first write type
            fwrite( pi, sizeof(*pi), fp); // then write data
            }
            void store(FILE *fp, D * pd)
            {
            int t = eTypeD;
            fwrite( &t, sizeof(int), 1, fp); // first write type
            fwrite( pd, sizeof(*pd), fp); // then write data
            }

            int main()
            {
            FILE * fp = fopen("data.raw", "wb");
            I i,j;
            D d,f;
            //..
            store(fp,i);
            store(fp,d);
            store(fp,j);
            //..
            }

            On reading, you've to first read the type of the stored data and then, according to the type, read the actual data. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            H Offline
            H Offline
            hrishiS
            wrote on last edited by
            #5

            thanks , that's the exact one i was looking for...In fact I dont have knowledge on serialization, But i have also reached t that point..Now I am having problem on reading... just for ur information.....my incomplete code is given below I have used class instead of struct class tableSructureUnknown { }; class tableSructure1: public tableSructureUnknown {     public:      int x;      int y; }; class tableSructure2: public tableSructureUnknown { public:      int x2;      int y2; }; class listTable { public:      int id;      int size;      char tName[20]; }; listTable ArrTableInfo[]= {          {TFX_TABLE1,sizeof(tableSructure1), "first"},               {TFX_TABLE2,sizeof(tableSructure2),"Second"},               {TFX_UNKNOWN,0,"Unknown"} }; FILE *ptr_myfile; int writeToFileTable(int tableId,tableSructureUnknown *lpbaseTableClass) {      ptr_myfile=fopen("test.bin","w");      if (!ptr_myfile)      {           printf("Unable to open file!");           return 1;      }      for(int i = 0;   (ArrTableInfo[i].id != TFX_UNKNOWN) || (ArrTableInfo[i].id != tableId) ;i++)      {           fwrite(&ArrTableInfo[i], sizeof(listTable), 1, ptr_myfile);           ptr_myfile=fopen("test.bin","w");           if (!ptr_myfile)           {                printf("Unable to open file!");                return 0;           }                                        fwrite(&lpbaseTableClass, ArrTableInfo[i].size , 1, ptr_myfile);

            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