I am trying to write a program that will open a specific file and go through it, writing the data I need to another file. Everytime the tag 'Step 4[2]' comes up I need to get the next two numbers and write them to a seperate file. (the purpose is to get the data to create a load displacement curve for an atomic simulation we're running) The results file is periodic, meaning that it repeats itself many times so the inefficient way would be to figure out how many lines are repeated and ignore that many before getting the data. The problem is, each simulation will have a different amount of lines needing to be skipped so it would take a lot of work to change the code for each different simulation. I'm hoping to write a more general code that can be used for all the simulations.

So here is my question: is there any way to ignore to a specific sequence of characters? (ie: ignore until it hits 'Step 4[2]')

I tried "inStream.ignore(4000, 'Step 4[2]');" but it give me errors because 'Step 4[2]' is more than one character. Is it possible to ignore until a specific sequence of characters instead of to a single character? Any help is GREATLY appreciated!!

Try to adopt this improvisation:

const size_t allbut = std::numeric_limits<int>::max();  
bool overStep(std::istream& in)
{
    char    c;
    while (in.ignore(allbut,'S')) {
        do 
        if (in.get(c) && c == 't' &&
            in.get(c) && c == 'e' &&
            in.get(c) && c == 'p' &&
            in.get(c) && c == ' ' &&
            in.get(c) && c == '[' &&
            in.get(c) && c == '2' &&
            in.get(c) && c == ']')
            return true;
        while (in && c == 'S');
    }
    return false;
}
// mini-test
int main()
{
    std::ifstream fin("step.txt");
    std::string  after;
    while (overStep(fin)) {
        if (fin >> after)
            std::cout << after << '\n';
    }
    return 0;
}
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.