Hi,

I am trying to parse a txt file in C++. I usually use perl to parse thru text files but need to do it in c++.

Here is my text file.

NAME	BITMAPVBIREFE
MESSAGE	VirginBitMapiIrefE
VCCVALUE	3.000000
VIHSELECT	1
VIHVALUE	3.000000
WPSELECT	1
WPARGSELECT	0
WPVALUE	2.600000

I want to be able to parse the element related to the name and store it in a class. My class has setter functions to set the value of the element. I want to parse for instance VCCVALUE and retrieve 3.00000 and store that in a VccValue data member in my class. Can anyone tell me how to parse for the number. Thanks.

Recommended Answers

All 2 Replies

Read the file line by line, split it into tokens based on spaces, then convert the number token into a double from a string.

You may want to look at.

getline()
strtok()
strtod()

Hope that helps,
Chris

commented: Thanks for the response. +1

Another option would be to use a stringstream to parse the line once it's been read in using getline().

commented: Thanks a bunch! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.