Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 generic code for writing structures, help me make it more generic
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chopficaro




PostPosted: Tue Sep 16, 2008 12:01 pm   Post subject: generic code for writing structures, help me make it more generic

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
code:
#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[]="\nplease enter 4 grades separated by pressing enter\n";
        char menu[]="\ndo u want to \n1, read from the file, or \n2, write over the file or \n3, append the file or \n4, exit the program?\n";
        //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
code:
#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 << "\nUnable to open file for size check\n";
        //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 << "\nUnable to open file for input\n";
}


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 << "\nUnable to open file for output\n";
}

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 << "\nUnable to open file for output\n";
}

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<<"\ngrade1\n"<< (pStructi[i]).grade1<<"\ngrade2\n"<< (pStructi[i]).grade2<<"\ngrade3\n"<< (pStructi[i]).grade3<<"\ngrade4\n"<< (pStructi[i]).grade4<<"\n";
        }
}


exportimportstructdec.h
code:
#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
Sponsor
Sponsor
Sponsor
sponsor
Rigby5




PostPosted: Thu Oct 09, 2008 10:43 pm   Post subject: RE:generic code for writing structures, help me make it more generic

Not too bad of a start.

The difference between classes and structs is really insignificant, in how the default public vs private.
So you don't really need to use classes.
You can add member functions to a struct as well.
But not many people do that, so I would switch to classes if you need member functions to manipulate the data in a group like a struct or class.

So how do you make it more generic?
Right now you have a fixed number of 4 integers in the struct.
A more generic way would be to have an integer that tells you how many values is stored, and then have an array of that size that actually has the integers in it.
That way the number can grow or shrink as necessary.
Initially the counter value would be 0, and the array would be of size 0, or just a pointer of type integer.

But every time you wanted to change the amount of data, you still would need make a new array, copy over any old data to the new array of a different size, and delete the old array.

A better way is to not use arrays at all, but to make a linked list. Each node is a struct with only a single data value, and a pointer to the next struct in the list.
If you don't want to learn about linked lists, then you could use an existing one, like from the Standard Template Library, (STL), which has things like VECTOR, etc.
OneOffDriveByPoster




PostPosted: Fri Oct 10, 2008 8:55 pm   Post subject: Re: generic code for writing structures, help me make it more generic

Maybe what you are looking for are templates, something like:
c++:
template <typename T>
void wrtfile(char const *const wrtFileName, std::string const &wrtMessage, T *const pStruct) {
    // ...
    // sizeof(T)
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: