This is coming from a noob on file handling, but I always get the same error message when I get my 'ifstream's and 'ofstream's mixed up. So you might want to double check that.
One you read the line into buf you use a variety of searches on the string buf to find the data you are looking for. For example, if your data is organized by line as follow.
You might modify your while statement to the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
std::string buf;
int pos;
while(getline(fileIn,buf){
pos=buf.find(','); //Searching the comma separator. Pos now stores its position
ResidentRecords[counter].socialSecurityNumber=buf.substr(0,pos);
buf.erase(0,pos+1); //We use +1 to erase the commas as well
pos=buf.find(','); //Find the next comma
ResidentRecord[counter].lastName=buf.substr(0,pos);
buf.erase(0,pos+1);
pos=buf.find(',');
ResidentRecord[counter].phoneNumber=buf.substr(0,pos);
buf.erase(0,pos+1);
/*Having no more commas we can just store the rest of buf into the struct*/
ResidentRecord[counter].typePhone=buf; /*You have typePhone as an enum so you might need to convert this to an int or some other data type depending on your needs./*
counter++;
/*The loop will now start back over with the NEXT line of data. It looks like you need to incriment counter, which you did not do here in this function.*/
}
In truth I usually do it slightly differently than what I have shown above. I nest another while loop WITHIN the one above as follow
1 2 3 4 5
while(getline(textIn,buf){
while (!buf.empty()){
//Retrieve data here
}
}
The two while loop method may require the use of a different data type so you may prefer not to use it.
void ReadAndStoreInputFile (ifstream& textIn, RecordType ResidentRecords [], int& counter)
{
// while file is still readable, and peek does not find end-of-file
while (textIn.good () && textIn.peek () != EOF)
{
// if the file contains too many lines of data, stop reading data and issue warning
if (counter >= MAX_RESIDENTS)
{
cout << "Error: file contains too many lines of data" << endl;
}
else
{
// read in data from RESIDENTS.TXT
textIn >> ResidentRecords [counter].socialSecurityNumber;
textIn >> ResidentRecords [counter].lastName;
textIn >> ResidentRecords [counter].phoneNumber;
textIn >> (int&) ResidentRecords [counter].typePhone;
counter++;
}
// read newline
textIn.get ();
}
}