I have a poll-class which needs to be able to save itself in a file and load it again. Everything works except for one call of getline(ifile, lineBuffer)
in the implementation of loadPoll
. (Marked with "<==========="). It should read the 9th line of the example file into lineBuffer however my debugger shows the string to be empty in this case only. On all other calls of getline()
it reads the correct line. What am I doing wrong?
This is the mo_poll class and pollOption struct:
typedef struct{
int id;
int voteCount = 0;
std::string value;
std::vector<std::string> voterIDs;
bool opt_hasVoted(const std::string& voterID);
}pollOption;
class mo_poll
{
public:
//unrelated constructors and methods
void loadPoll(const std::string& filePath);
void savePoll(const std::string& filePath);
std::string topic;
std::vector<pollOption> options;
//data
std::string author = "";
std::string pollChannelID = "";
std::string pollMessageID = "";
bool isSaved = false;
bool messageExists = false;
bool isClosed = false;
bool allowCustOpt = false;
bool allowMultipleChoice = false;
private:
int nextID = 1;
};
The loadPoll() method is defined like this:
void mo_poll::loadPoll(const std::string &filePath){
std::ifstream ifile;
ifile.open(filePath);
if(!ifile.is_open()){return;}
//Topic:
std::string lineBuffer;
getline(ifile, lineBuffer);
lineBuffer.erase(lineBuffer.begin());//Remove "'"
lineBuffer.pop_back();//Remove last "'"
topic = lineBuffer;
//Author:
getline(ifile, lineBuffer);
lineBuffer.erase(lineBuffer.begin());//Remove "'"
lineBuffer.pop_back();//Remove last "'"
author = lineBuffer;
//pollChannelID:
getline(ifile, lineBuffer);
pollChannelID = lineBuffer;
//pollMessageID:
getline(ifile, lineBuffer);
pollMessageID = lineBuffer;
//messageExists:
getline(ifile, lineBuffer);
messageExists = intToBool(std::stoi(lineBuffer));
//isClosed:
getline(ifile, lineBuffer);
isClosed = intToBool(std::stoi(lineBuffer));
//allowCustOpt:
getline(ifile, lineBuffer);
allowCustOpt = intToBool(std::stoi(lineBuffer));
//allowMultipleChoice:
getline(ifile, lineBuffer);
allowMultipleChoice = intToBool(std::stoi(lineBuffer));
//First explicitly saved optionID:
getline(ifile, lineBuffer);
int lastOptID = std::stoi(lineBuffer);
//Load all options data:
bool firstLineOfOption = true;
while(!ifile.eof()){
getline(ifile, lineBuffer);//'lineBuffer' is empty but it shouldn't <=============
if(lineBuffer.size() > 0){
//Get optionID and values:
std::regex idReg("\\d+");
int currOptID = std::stoi(returnMatches(lineBuffer, idReg)[0]);
std::regex valReg("'.+'");
auto vals = returnMatches(lineBuffer, valReg);
std::string currOptValue = vals[0];
currOptValue.erase(currOptValue.begin());
currOptValue.pop_back();
pollOption currOption;
if(currOptID == lastOptID){
currOption.id = currOptID;
if(firstLineOfOption){
currOption.value = currOptValue;
firstLineOfOption = false;
}else{
currOption.voterIDs.push_back(currOptValue);
}
}
else{
firstLineOfOption = true;
options.push_back(currOption);
currOption = pollOption();//Empty currOption
}
}
}
ifile.close();
}
Here is an example file:
'Test Poll'
'Some_Author_Name'
123456789012345678
123456789012345678
1
0
0
1
1
o 1 'Yes.'
o 2 'No'
o 2 '123456789012345678'
o 3 'Maybe'
o 4 'Something else'
o 4 '123456789012345678'