Feb 25, 2017 at 6:19pm UTC
I managed to read data from the textfile that uses `:` as a delimter. I'm able to get the data from the text file, store into the struct and vector and obtain the size as shown below.
int InventoryDatabase::readToArray (const char* filename) {
fstream afile;
afile.open (filename, ios::in);
entry e;
while(getline(afile, tempstring))
{
string tempID = tempstring.substr(0,tempstring.find(delimiter));
tempstring.erase(0, tempstring.find(delimiter) + delimiter.length());
e.stockID = ( strToNum(tempID) );
string tempItem = tempstring.substr(0,tempstring.find(delimiter));
tempstring.erase(0, tempstring.find(delimiter) + delimiter.length());
e.stockItem = ( tempItem );
vectorOfEntry.push_back(e);
}
afile.close();
return vectorOfEntry.size(); // return 350
}
I tried to get the size of the vector , it return me 0. It also doesn't store the data in the vector into the textfile
int InventoryDatabase::writeToFile (const char* filename){ //instance of the class InventoryDatabase
fstream afile;
afile.open(filename, ios::out | ios::app);
int size = vectorOfEntry.size(); // extract number of struct in vector
for (int i = 0; i < size; i++) // data unable to store into text file
{
afile << vectorOfEntry[i].stockID
<< ":"
<< vectorOfEntry[i].stockItem;
}
afile.close();
return size; // return 0
}
This is how i store the struct of vector in my class file. my struct is stored globally while my vector is stored in a protected sector.
struct entry //example of some data of the struct
{
int stockID;
string stockItem;
};
class InventoryDatabase {
public:
InventoryDatabase();
int writeToFile (const char* filename);
protected:
vector<entry> vectorOfEntry;
};
This is my main function where i will use the `writeToFile` function to write the necessary data into the textfile
int main()
{
InventoryDatabase inventDB;
int writeSize, readSize;
readSize = inventDB.readToArray("SampleData.txt");
cout << "Successful reading! Size is : " << readSize << endl; // display 350
writeSize = inventDB.writeToFile("writeFile.txt");
cout << "Successful writing ! Size is : " << writeSize << endl; // displays 0
}
Last edited on Feb 25, 2017 at 6:20pm UTC