A Runtime error while using Substr

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');


Any help?
Last edited on
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.
OK, that was the problem. I used if to output Invalid Input if it can't find IDnum.
It couldn't find it.

But I can't understand why. This is the file:

10001.
Name: ABC
Gender: Male
Age: 20
Height: 200
Profession: student


I have searched for 10001 and 10001.
but it gives me the same reply that it couldn't find IDnum.

BTW, this is how I checked for IDnum:
1
2
3
4
if (IDposition != string::npos)
	{
		Dat = Full_Data.substr(IDposition);
	}


Any idea on why it wouldn't be able to find that value?
NOTE:IDnum is a string
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.

And yes, the first line was intentional
Last edited on
Try double back slashes

ifstream DataRetrive ("F:\\C++\\C++ Programs\\People Data Holder\\People Data Holder\\PersonData.txt");
Last edited on
Well, tried that as well now. The problem persists.
Full_Data in blank!

The Path of the Folder the file is in is:
F:\C++\C++ Programs\People Data Holder\People Data Holder

The File's name is PersonData.txt, copied from the ostream file name

PersonData is being updated, it already has 6 records.

This ensures the file path and the name are correct!
But why isn't the file reading from the file?

EDIT: Got it working. The error was in reading the file.
Last edited on
Topic archived. No new replies allowed.