generic code for writing structures, help me make it more generic

cpp Viet Nam
  • 9 years ago
    well ive got a program that scans a structure from cin and puts it in a binary file, and can also take it out of the binary file and display it. i like the interface ive made very much, i want to use this code for many many projects without having to change much,

    especially for many different types of structures,

    so im trying to make it more generic, but i havent a clue how to proceed. my guess is that ill have to use some element of classes.

    im not asking u to write my code i just want a hint. anything at all would be greatly appreciated

    take a look:

    exportimportstructmain.cpp
    #include <iostream>
    #include <fstream>
    #include "exportimportstructdec.h"
    using namespace std;

    grades myGradesi,myGradeso,myGradesa;

    //declare pointers to structures for file io:
    grades * pMyGradeso=&myGradeso;
    grades * pMyGradesa=&myGradesa;
    grades * pMyGradesi;

    int main ()
    {
        int choice;
        char opFile[]="binary.bin";
        char opMessage[]=" please enter 4 grades separated by pressing enter ";
        char menu[]=" do u want to 1, read from the file, or 2, write over the file or 3, append the file or 4, exit the program? ";
        //give the user options:
        do
        {
            cout<<menu;
            cin>>choice;
            //option 1, read from the file:
            if (choice==1)
            {
                readfile(opFile,myGradesi,pMyGradesi);
            }
            //option 2, write over the file:
            if (choice==2)
            {
                wrtfile(opFile,opMessage,myGradeso,pMyGradeso);
            }
            //option 3, append the file:
            if (choice==3)
            {
                appfile(opFile,opMessage,myGradesa,pMyGradesa);
            }
        //option 4, exit:
        }while (choice!=4);
        return 0;
    }

    exportimportstructdef.cpp
    #include <iostream>
    #include <fstream>
    #include "exportimportstructdec.h"
    using namespace std;

    extern grades myGradesi, myGradeso, myGradesa;
    extern grades *pMyGradeso, *pMyGradesa, *pMyGradesi;

    //the purpose of this program is to send structures into a file, and then read them from the same file
    //structure to be written:


    void readfile(char readFileName[],grades structi,grades * pStructi)
    {
        //check the size of the file and allocate appropriate memory:
        long begin,end;
        ifstream filecheck (readFileName);
        if (filecheck.is_open())
        {
            begin = filecheck.tellg();
            filecheck.seekg (0, ios::end);
            end = filecheck.tellg();
            filecheck.close();
            int fileSize=(end-begin)/(sizeof(structi));
            pStructi= new grades [fileSize];
        }
        else cout << " Unable to open file for size check ";
        //read the file into memory:
        int structArraySize=0;
        ifstream filei;
        filei.open (readFileName, ios::in|ios::binary);
        if (filei.is_open())
        {
            while ((filei.peek()!=EOF))
            {
                filei.read(reinterpret_cast<char*>(&pStructi[structArraySize]), sizeof(grades));
                structArraySize++;
            }
            //display memory on screen:
            printmemory(structArraySize,pStructi);
        }
        else cout << " Unable to open file for input ";
    }


    void wrtfile(char wrtFileName[],char wrtMessage[],grades structo,grades * pStructo)
    {
        ofstream fileo;
        fileo.open (wrtFileName, ios::out|ios::binary);
        cout << wrtMessage;
        scanstruct(pStructo);
        if (fileo.is_open())
        {
            fileo.write (reinterpret_cast<char*>(pStructo), sizeof(structo));
            fileo.close();
        }
        else cout << " Unable to open file for output ";
    }

    void appfile(char appFileName[],char appMessage[],grades structa,grades * pStructa)
    {
        ofstream filea;
        filea.open (appFileName, ios::app|ios::binary);
        cout << appMessage;
        scanstruct(pStructa);
        if (filea.is_open())
        {
            filea.write (reinterpret_cast<char*>(pStructa), sizeof(structa));
            filea.close();
        }
        else cout << " Unable to open file for output ";
    }

    void scanstruct(grades * pStructscan)
    {
        cin >> (*pStructscan).grade1 >> (*pStructscan).grade2 >> (*pStructscan).grade3 >> (*pStructscan).grade4;
    }

    void printmemory(int specificStructArraySize,grades * pStructi)
    {
        for(int i=0;i<specificStructArraySize;i++)
        {
            cout<<" grade1 "<< (pStructi[i]).grade1<<" grade2 "<< (pStructi[i]).grade2<<" grade3 "<< (pStructi[i]).grade3<<" grade4 "<< (pStructi[i]).grade4<<" ";
        }
    }


    exportimportstructdec.h
    #ifndef EXPORTIMPORTSTRUCTDEC_H
    #define EXPORTIMPORTSTRUCTDEC_H

    struct grades
    {
        int grade1;
        int grade2;
        int grade3;
        int grade4;
    };


    void printmemory(int ,grades * );
    void scanstruct(grades * );
    void readfile(char [],grades ,grades * );
    void wrtfile(char [],char [],grades ,grades * );
    void appfile(char [],char [],grades ,grades * );

    #endif
  • 9 years ago

    im afraid i dont know what that nonsense   is where all of my tabs are and cant get rid of it

  • 9 years ago

     Hi chopficaro,

    I had a problem like yours, when I pasted some text from MS Word.

    I suggest, just paste your code from VS or a .txt file, or just type that here (first clear all of your post).

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky