I am retrieving data from a file using getline.
Then I am using find to look for an IDNumber in the string.
And use that to put it into a substring, from that point to the end.
Finally I put that string into a stringstream and then use the delimiter in getline to get the first Entry in the substr.
But on runtime I get this error:
std::out_of_range at memory location
The relevant part of the code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
ifstream DataRetrive ("PersonData.txt");
while (DataRetrive.good())
{
getline (DataRetrive,Full_Data);
}
DataRetrive.close();
cout << endl << endl << "Please enter the ID of the person whose record you want to see: ";
cin >> IDnum;
cin.ignore();
IDposition = Full_Data.find(IDnum);
Dat = Full_Data.substr(IDposition); //The error doesn't occur if I comment out this line
std::stringstream Data_file;
Data_file << Dat;
getline (Data_file,Dat,'\r');
If Full_Data.find(IDnum); didn't find IDnum then it would return std::string::npos. Passing this into substr would cause the out of range error. I guess that it just isn't finding IDnum. If this is the case, examine Full_Data and IDnum for clues as to why it wouldn't be finding it.
Theoretically, it should find 10001 (the position would be 1 assuming the blank line is intentional). Did you check the string, Full_Data? Does it contain the contents of the file?
I would also wager that this ifstream DataRetrive ("PersonData.txt"); is not finding the file "PersonData.txt". Try also including the path.
I checked the string, Full_Data doesn't contain any data. I will try it with the path.
EDIT:It isn't working after inclusion of the path. I put in the path like this: ifstream DataRetrive ("F:\C++\C++ Programs\People Data Holder\People Data Holder\PersonData.txt");
where the path was copied from the address bar in the Explorer.