Hi everyone, I am having a difficulty with some code and I hope here somebody who is more expreinced from me can help me.
I am having a program that reads a file store the data as objects in a vector.
For example in the file i have :
Once upon a time in the west?#1#1#1#1#2# which is my object called Question with fields description, answerA, answerB, answerC, answerD, difficulty.
i am reading the data from the file like this :
string str[6];
string end="";
int flevel=0; //variable to type_cast the dificulty
input_file.clear();
input_file.seekg(ios_base::beg);
while (input_file.good())
{
getline(input_file,str[0],'#');
getline(input_file,str[1],'#');
getline(input_file,str[2],'#');
getline(input_file,str[3],'#');
getline(input_file,str[4],'#');
getline(input_file,str[5]);
flevel=atoi(str[5].c_str());
temp_question->Add_data(flevel,str[0],str[1],str[2],str[3],str[4]);
//add data in the object
fdata->push_back(*temp_question);
//add the object in the list with questions
}
after that my data is in the my vector , where i can work with it.
after i do my modifications i am saving it to the same file like that :
ofstream output(filename);
if(output.is_open())// check if file is open
{
vector<Question>::iterator it;
for(it=fdata->begin();it!=fdata->end();it++)
{
it->SaveFile(output);
}
cout<<"\nData saved in succesfully saved in the file!\n";
}
else
{
cout<<"\nFailed to read Input file\n"<<endl;
}
output.close();
where SaveFile(output) is a function from my class and it does this :
foutput<<description<<'#'<<answerA<<'#'<<answerB<<'#'<<answerC<<'#'
<<answerD<<'#'<<difficulty<<endl;
So my problem is the next: When i start the program for first time i load this data and i have N questions(in my case i have only 7 so far in the file). After that i save it and when i start it again i have N+1 questions because the end of the file is a new line. This new question have all the data from the previos one exept for the description.
For example if my last question is
Once upon a time in the west?#1#1#1#1#2# and i have one new line more in the file
in the vector i have "Question"object that has all these field except the description which is that part="Once upon a time in the west?"
Can you help me how to repare that bug in my program?
Thank you in advance :)