Hi, I am writing a program which needs to extract data from a file.

Sample file
Adder: 4, yes
Mult: 6,yes
....

i want to extract this info into a structure

{
  Int interval (4 should be entered here)
  bool pipeline (if yes then 1 else 0)
}

i read the file using the getline function and need to know a way i can search the string to extract the data from the string and store it in the structure above

string line;
if(Instfile.is_open())
{
    while(!Instfile.eof())
    {
        LineNo++;
        getline(Instfile,line);     
        }
}

Here is one way to do it

#include <string>
#include <sstream>
using namespace std;


int main()
{
    string s = "Adder: 4, yes";
    stringstream str(s);
    string m;
    int n;
    bool b;
    str >> m; // "Adder:"
    str >> n;  // 4
    str >> m >> m; // extract comma and the word "yes"
    if( m == "yes")
        b = true;
    else
        b = false;
}
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.