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 intergers, because the function cant SEE the integers, it makes sense that it would be blind untill runtime
the origional 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? the ultimate goal is to allow the user to enter as many ints, floats, and strings that he wants into the file.
heres my code:
//exportimportstructdec.h
#include <iosfwd>
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();
};//exportimportstructdef.cpp
#include <iostream>
#include <fstream>
#include <string>
#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.is_open())
{
fileOpenWright.write (reinterpret_cast<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").c_str(), ios::in|ios::binary);
if (filei.is_open())
{
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";
}//exportimportstructmain.cpp
#include <iostream>
#include <fstream>
#include <string>
#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;
cin>>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;
}
This post has been edited by chopficaro: 27 May 2009 - 12:29 AM

Help


Back to top










