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. reading writing to a file system

reading writing to a file system

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmshelplearning
7 Posts 4 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

    Anyone please help me to read write 3 differnt structure to a file...and read them searching the file....like struct A { int id; int data; }; struct B { int id; float data; }; struct C { int id; double data; int data2 };

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

    A R CPalliniC 3 Replies Last reply
    0
    • H hrishiS

      Anyone please help me to read write 3 differnt structure to a file...and read them searching the file....like struct A { int id; int data; }; struct B { int id; float data; }; struct C { int id; double data; int data2 };

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

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      I think you to check with serialization. Please check A serialization primer - Part 1[^]

      Величие не Бога может быть недооценена.

      H 1 Reply Last reply
      0
      • H hrishiS

        Anyone please help me to read write 3 differnt structure to a file...and read them searching the file....like struct A { int id; int data; }; struct B { int id; float data; }; struct C { int id; double data; int data2 };

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

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        You mean plain text? You could keep every such struct as a "section" and use an .inf file to store the values. For example, for a string value, you could use WritePrivateProfileString[^] (and there's an 'int' variant, and so on). For reading, use GetPrivateProfileString[^]. This would be plain text and readable. If you need encryption, you must take care of it. You might as well serialize[^] your data.

        “Follow your bliss.” – Joseph Campbell

        H 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          You mean plain text? You could keep every such struct as a "section" and use an .inf file to store the values. For example, for a string value, you could use WritePrivateProfileString[^] (and there's an 'int' variant, and so on). For reading, use GetPrivateProfileString[^]. This would be plain text and readable. If you need encryption, you must take care of it. You might as well serialize[^] your data.

          “Follow your bliss.” – Joseph Campbell

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

          thanks a lot...but i need a file system only ...not INI file...preferably a binary file

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

          1 Reply Last reply
          0
          • A Adam Roderick J

            I think you to check with serialization. Please check A serialization primer - Part 1[^]

            Величие не Бога может быть недооценена.

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

            thanks... I quickly read the article, its somewhat interesting...I need to write the different type of class..not only the one....in that case please help me ... One more important thing is that I am not using MFC...its a pure C++ console based application

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

            modified on Thursday, December 17, 2009 12:42 AM

            A 1 Reply Last reply
            0
            • H hrishiS

              thanks... I quickly read the article, its somewhat interesting...I need to write the different type of class..not only the one....in that case please help me ... One more important thing is that I am not using MFC...its a pure C++ console based application

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

              modified on Thursday, December 17, 2009 12:42 AM

              A Offline
              A Offline
              Adam Roderick J
              wrote on last edited by
              #6

              Just check this http://www.osix.net/modules/article/?id=348[^]

              Величие не Бога может быть недооценена.

              1 Reply Last reply
              0
              • H hrishiS

                Anyone please help me to read write 3 differnt structure to a file...and read them searching the file....like struct A { int id; int data; }; struct B { int id; float data; }; struct C { int id; double data; int data2 };

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

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

                I already gave you some code, but OK, it was incomplete. Let's try a plain, not object oriented, C-Like approach:

                #include

                enum SerialType
                {
                eTYPE_UNRECOGNIZED,
                eTYPE_A,
                eTYPE_B,
                eTYPE_C,
                eTYPES
                };

                struct A
                {
                int id;
                int data;
                };

                struct B
                {
                int id;
                float data;
                };

                struct C
                {
                int id;
                double data;
                int data2;
                };

                size_t getSize(SerialType st)
                {
                switch( st)
                {
                case eTYPE_A:
                return sizeof(A);
                case eTYPE_B:
                return sizeof(B);
                case eTYPE_C:
                return sizeof(C);
                default:
                return 0;
                }
                }

                bool store( SerialType st, void * pData, FILE * fp)
                {
                int type = st;
                size_t size = getSize(st);

                if (!fp || !pData || !size) return false;
                fwrite(&type, sizeof(type), 1, fp);
                fwrite(pData, size,1,fp);
                return true;
                }

                SerialType readType( FILE * fp)
                {
                SerialType type;
                if (!fp ) return eTYPE_UNRECOGNIZED;

                if ( fread(&type, sizeof(type), 1, fp) == 0 ) return eTYPE_UNRECOGNIZED;
                return type;
                }

                bool readData( SerialType st, void * pData, FILE * fp)
                {
                size_t size = getSize(st);
                if ( !fp || !pData || !size ) return false;
                fread(pData, size, 1, fp);
                return true;
                }

                void main()
                {
                SerialType type;

                A a[2];
                B b;
                C c1,c2;

                A * pa=NULL;
                B * pb=NULL;
                C * pc=NULL;

                // define some variables
                a[0].data = 5;
                a[0].id = 12;

                a[1].data = 6;
                a[1].id = 13;

                b.data=25.75f;
                b.id=100;

                c1.data = 27;
                c1.data2=35;
                c1.id = 7;

                c2.data=1000.372;
                c2.data2=1000;
                c2.id=1;

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

                // store some of the defined variables
                store(eTYPE_C, &c2, fp);
                store(eTYPE_A, &a[1], fp);
                store(eTYPE_B, &b, fp);
                store(eTYPE_A, &a[0], fp);

                fclose(fp);

                // read the variables from the storage, printing their values.
                fp = fopen("data.raw", "rb");

                while ((type=readType(fp)) != eTYPE_UNRECOGNIZED)
                {
                switch (type)
                {
                case eTYPE_A:
                pa = new A();
                readData(type, pa, fp);
                printf("type A, id=%d, data=%d.\n", pa->id, pa->data);
                delete pa;
                break;
                case eTYPE_B:
                pb = new B();
                readData(type, pb, fp);
                printf("type B, id=%d, data=%g.\n", pb->id, pb->data);
                delete pb;
                break;
                case eTYPE_C:
                pc = new C();
                readData(type, pc, fp);
                printf("type C, id=%d, data=%g, data2=%d.\n", pc->id, pc->data, pc->data2);
                delete pb;
                break;
                }
                }
                fclose(fp);
                }
                <

                In testa che avete, signor di Ceprano?

                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