简体   繁体   中英

Save next word, if a given word is found (C++)

I'm pretty new to C++. I have a text doc that looks like this:

InputFile.txt

...
.
..
.
.
....
 TIME/DISTANCE =      500/ 0.1500E+05
..
..
.
 ...
 TIME/DISTANCE =      500/ 1.5400E+02
.
...
...
.
 TIME/DISTANCE =      500/ 320.0565
..
..
.
.
...

The one line shown keeps repeating throughout the file. My objective is to save all the numbers after the 500/ into an array/vector/another file/anything. I know how to read a file and get a line:

string line;
vector <string> v1;
ifstream txtfile ("InputFile.txt");
if (txtfile.is_open())
{
    while (txtfile.good())
    {
        while( getline( txtfile, line ) )
        {
            // ?????
            // if(line.find("500/") != string::npos)
            // ?????
        }
    }
    txtfile.close();
}

Does anybody have a solution? Or point me in the right direction?

Thanks in advance.

Edit: Both proposed solutions (Jerry's and Galik's) work perfectly. I love this community. :)

This is one of those rare cases that (IMO) it may make sense to use sscanf in C++.

std::string line;
std::vector<double> numbers;

while (std::getline(txtfile, line)) {
    double d;
    if (1==sscanf(line.c_str(), " TIME/DISTANCE = 500 / %lf", &d))
        numbers.push_back(d);
}

This takes each line, and attempts to treat it as having the format you care about. Where that succeeded, the return value from sscanf will be 1 (the number of items converted). Where it fails, the return value will be 0 (ie, it didn't convert anything successfully). Then we save it if (and only if) there was a successful conversion.

Also note that sscanf is "smart" enough to treat a single space in the format string as matching an arbitrary amount of white-space in the input, so we don't have to try to match the amount of white space precisely.

We could vary this somewhat. If there has to be a number before the '/', but it could be something different from 500 , we could replace that part of the format string with %*d . That means sscanf will search for a number (specifically an integer) there, but not assign it to anything. If it finds something other than an integer, conversion will fail, so (for example) TIME/DISTANCE ABC/1.234 would fail, but TIME/DISTANCE 234/1.l234 would succeed.

When processing your line then you can use line.find() to check its the right line and to find your data:

if(line.find("TIME/DISTANCE") != std::string::npos)
{
    // this is the correct line
}

Once you have the correct line you can get the position of the data like this:

std::string::size_type pos = line.find("500/");

if(pos != std::string::npos)
{
    // pos holds the position of the numbers you want

    std::string wanted_numbers = lint.substr(pos + 4); // get only the numbers in a string
}

Hope that helps

EDIT: Fixed bug (adding 4 to pos to skip over the "500/" part)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM