Extracting values from file

Hi.

In the file "myfile.txt", I have data like so:
1
2
3
Name : John
Sex : Male
Age : 20


I wanted to extract the value for Name "John" and store into a variable

I have tried:
1
2
3
4
5
6
7
8
9
10
11
12
fstream myfile("myfile.txt");
while(getline(myfile, templine))
{
    line.append(templine);
}
//find values

locate = line.find("Name : ", 0); //location of the keyword
value_id_start = locate + 7; //start for location of the value "John"
end_location = line.find(" ", value_id_start); //gets location of the space after "John"
val_length = end_location - value_id_start; //gets value length
Name = line.substr(Val_id_start, val_length);//stores the value into variable 'Name' 


But it wouldn't work since the line "Name : John" the end_location is a carriage return or to the next line, so my "end_location" get messed up.

Any idea how could i get the value "John"? or is there any better ways.

Thanks in advance.
You are adding too much to value_id_start which means that john isn't being found at all. You are adding 7 which means that you start searching for the " " where the name actually starts and of course it will not be found.

I edited this to delete my other suggestions. You haven't posted enough of the program for me to understand your requirements well enough. There are many ways to parse a file. Pangalactic's example is good because it shows you how you can avoid using magic numbers within your code and simply take advantage of the std::string interface which already tells you what you need to know.
Last edited on
Something like this should work:

1
2
3
4
5
6
7
const std::string label = "Name : ";
std::string name;
size_t pos = line.find(label);
if (pos != line.npos)
{
    name = line.substr(label.size());
}
Thanks for the replies.

I have tried PanGalactic's method, but it doesn't work. I get segmentation fault.

Kempofighter:

With regards the the codes,
locate -> will get the position where "N" of "Name : " is,
value_id_start = locate + 7 -> adding 7 becomes the position of " ", the last character from "Name : " is.
then
end_location = line.find(" ", value_id_start) -> the find will to find a blank space " ", starting from position of end_location, which for example:
1
2
3
4
5
6

Name : John

if location of 'N' is 1 for example, and location of the last character 'n' of John is 11.
end_location=line.find(" ", value_id_start) mean will find the " " starting from position 7.
so the first " " that it find starting from position 7 is position 12, which is after 'John'


and if i get the end_location minus the value_id_start, i get the length of the word "John"
then if i do "line.substr(value_id_start, val_length), i get the word John.

I have tried this is other types of string where it has a whole line of word and i extract values between the words and it works.

problem now is the end_location to find " " will go all the way to the next line since the character after the word "John" is not " " but instead is a 'carriage return' which will go to the next line.

Sorry for my bad explanation, I hope you would understand what i mean.

Thanks again
Topic archived. No new replies allowed.