C++ insert a USER DEFINED class into a file, then read it

file , class , user defined class Lowell, United States
  • 11 years ago

    i'm trying to make a program that can insert a class into a file, the contents of which are USER DEFINED, those are the key words here, but im having trouble. so to get started off, i tried to just get a function to accept a variable of its own class that is dynamically allocated that seems to be the trouble, i get compiler errors at the point of defining the function that uses those dynamically allocated ints, because the function cant SEE the ints, it makes sense that it would be blind until runtime the original plan was to get this to work somehow and then: ask the user how many ints he wants put into the file the number of ints would be saved in file 1 then we would scan the ints from the user, and save them in file 2 then when it came time to read the ints back again, we would first read file 1 and newInts = new int [whatsInFile1] then read whats in file 2 into newInts

    does anyone have any suggestions? is there another way of doing this?

    heres my code:

    [code]

    //exportimportstructdec.h

    include

    using namespace std; class BinFileIO { void PrintMemory(); void PostOpenWrite(std::ofstream&,string); void ScanStruct(); public: void ReadFile(string); void WrtFile(string,string); void AppFile(string,string); void NewInts(); };

    [/code]

    [code]

    //exportimportstructdef.cpp

    include include include include "exportImportStructDec.h"

    using namespace std;

    void BinFileIO::NewInts() { int * atarashiInts; atarashiInts = new int [4]; }

    void BinFileIO::PostOpenWrite(ofstream& fileOpenWright,string openWriteMessage) { cout << openWriteMessage; this->ScanStruct(); if (fileOpenWright.isopen()) { fileOpenWright.write (reinterpretcast<char*>(this), sizeof(*this)); fileOpenWright.close(); } else cout << "\nUnable to open file for output\n"; } void BinFileIO::ReadFile(string readFileName) { ifstream filei; filei.open ((readFileName+".obj").cstr(), ios::in|ios::binary); if (filei.isopen()) { filei.read(reinterpret_cast<char*>(this), sizeof(BinFileIO)); this->PrintMemory(); } else cout << "\nUnable to open file for input\n"; }

    //function to write over a file with a new structure void BinFileIO::WrtFile(string wrtFileName,string wrtMessage) { ofstream fileo; fileo.open ((wrtFileName+".obj").c_str(), ios::out|ios::binary); this->PostOpenWrite(fileo,wrtMessage); }

    //function to append the end of a file with a new structure void BinFileIO::AppFile(string appFileName,string appMessage) { ofstream filea; filea.open ((appFileName+".obj").c_str(), ios::app|ios::binary); this->PostOpenWrite(filea,appMessage); }

    void BinFileIO::ScanStruct() { cin >> this->atarashiInts[0] >> this->atarashiInts[1] >> this->atarashiInts[2] >> this->atarashiInts[3]; }

    void BinFileIO::PrintMemory() { cout<<"\nint1\n"<< this->atarashiInts[0]<<"\nint2\n"<< this->atarashiInts[1]<<"\nint3\n"<< this->atarashiInts[2]<<"\nint4\n"<< this->atarashiInts[3]<<"\n"; }

    [/code]

    [code]

    //exportimportstructmain.cpp

    include include include include "exportImportStructDec.h"

    using namespace std;

    BinFileIO myGradesi,myGradeso,myGradesa;

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

    int main () { BinFileIO objectBinIO; int choice; string opFile; string opMessage="\nplease enter 4 Grades separated by pressing enter\n"; string 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<<"please enter the file name"; cin>>opFile; cout<<menu;>>choice; //option 1, read from the file: if (choice==1) { objectBinIO.NewInts();

    		objectBinIO.ReadFile(opFile);
    	}
    	//option 2, write over the file:
    	if (choice==2)
    	{
    		objectBinIO.NewInts();
    
    		objectBinIO.WrtFile(opFile,opMessage);
    	}
    	//option 3, append the file:
    	if (choice==3)
    	{
    		objectBinIO.NewInts();
    
    		objectBinIO.AppFile(opFile,opMessage);
    	}
    //option 4, exit:
    }while (choice!=4);
    return 0;
    

    }

    [/code]

  • 11 years ago

    im sorry please delete this, the one that is properly code wrapped is the one that should be saved

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.

“It works on my machine.” - Anonymous