Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Newbie Need help with C++ ifstream and array objects

hello,

I have a module i am working on it basically work like this, it takes the content of a text file and it put it into a array object. I can't seem to get it to work with the variable being private so i tried to create a get and even a set function and still having problems with maybe some out there can help. when i use my get function it causes an access violation or something. Here is the code below
[422 byte] By [lew26] at [2007-11-11 8:05:36]
# 1 Re: Newbie Need help with C++ ifstream and array objects
the problem is that you're dealing with char pointers. Try to use std::string instead. It will immensley simpler.
Danny at 2007-11-11 21:01:43 >
# 2 Re: Newbie Need help with C++ ifstream and array objects
That make a big different, do you know how to pass an object array like in the load form I want to declare

void __fastcall TfrmRecipeBook::FormCreate(TObject *Sender)
{
// pass this
cRecipeBook cRbObjArray[100];
cFileSystemClass fc;

// be able to pass it into this method byref
fc.GatherRecipeBook(cRbObjArray);

} // FormCreate

......from there

void cFileSystemClass::GatherRecipeBook(cRbObjArray) {

static int mCaseNum = 1;

// pass into this method
// call filemanager query() function
cFileManager t;
t.Query(cRbObjArray, mCaseNum);

// initalize RecipeBook Object
cRecipeBook rb;

// check if data has been entered correctly

cout << "Data entered from file:" << endl;
for (int i = 0; i <= 10; i++) {
// input data from i-th mRecipeBook
cout << arrayRb[i].getRecipeID() << " "
<< cRbObjarray[i].getRecipeType() << " "
<< cRbObjarrayRb[i].getRecipeOrigin() << endl;

}

} // gatherReciptBook

.....from there

// I am having problem here also I want to be able to access the private members of the class that is why i created the get and set functions so i can get and set the values fron the private variables. It doesn't seem to be working well with the fstream and the objArray

void cFileManager::Query(int caseNum)
{
// create object of RecipeBook
cRecipeBook rb;
cRecipeBook arrayRb[100];
//cRecipe arrayR[10];
//cIngredient arrayI[200];
//cEquipment arrayE[50];
//cFileManager arrayFM[4];
ifstream infile ("RecipeBook.txt", ios::in);

switch (caseNum) {

case 1:
if (!infile)
{
cout << "error: unable to open "
<< "RecipeBook.txt" << endl;
break;
}
else {
// having errors here need to fix

for (int i=0; i <= 10; i++) {
// input data from file into i-th mRecipeBook
infile >> arrayRb[i].getRecipeID();
infile >> arrayRb[i].getRecipeType();
infile >> arrayRb[i].getRecipeOrigin();
}

// check to see if have everything from file
if (infile.get() !=EOF) {
cout << "something wrong with input: there are still some data in the file"
<< endl;
}
}

break;

case 2:

break;

case 3:

break;

case 4:

break;

default:
cout << "program should never reach here" << endl;

}

// close the open file: DONT FORGET TO DO SO
infile.close();
cin.get();

// or if i have return the object array
// return cRbObjArray;

} // Query
lew26 at 2007-11-11 21:02:37 >