I have a program which writes data to a file and retrieves it back from the file. When I tried to populate an object using the retrieved values i found out that the values get retrieved as one with out separation. Meaning I have 2 variables named vegetableName and quantity. The data stored is stored in the format of vegetableName quantity.
Eg:- Potato 10
The problem is when am retrieving data from the file, the quantity gets attached to the name, so I cant populate the quantity variable since the quantity is attached to vegetableName.
Here is a screen shot of the program :
As you can in the above screen shot the quantity is attached with the name
But I want it as in the below screen shot
Here is my code for writing data to file:
string fileHandler::writeToFile(Vegetables writeObject)
{
ofstream outputFile;
outputFile.open("my.txt",ios::out|ios::app);
string dataToWrite=writeObject.toString();
outputFile.write(dataToWrite.c_str(),dataToWrite.size());
outputFile.close();
return "done";
}
Here is my file accessing code:
void fileHandler::readFromInventory()
{
Vegetables veg;
bool stat=true;
vegetableName;
int quantity;
fstream readFile("my.txt",ios::in);
if(!readFile)
{
cerr<<"File Unable to Open"<<endl;
exit(1);
}
while(true)
{
if(readFile.eof())
{
break;
}
readFile>>vegetableName>>quantity;
cout<<"Retrieved Vegetable Name:"<<vegetableName<<endl;
cout<<"Retrieved Quantity :"<<quantity<<endl;
...
What seems to be the problem here ?