简体   繁体   English

保存下一个单词,如果找到给定的单词(C ++)

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

I'm pretty new to C++. 我是C ++的新手。 I have a text doc that looks like this: 我有一个看起来像这样的文本文档:

InputFile.txt 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. 我的目标是将500/之后的所有数字保存到数组/向量/另一个文件/任何内容中。 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++. 这是(IMO)在C ++中使用sscanf可能有意义的极少数情况之一。

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). 如果成功, sscanf的返回值将为1(转换的项目数)。 Where it fails, the return value will be 0 (ie, it didn't convert anything successfully). 如果失败,返回值将为0(即,它没有成功转换任何内容)。 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. 另请注意, sscanf足够“智能”,可以将格式字符串中的单个空格视为与输入中任意数量的空白区域相匹配,因此我们不必尝试精确匹配空白区域。

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 . 如果在'/'之前必须有一个数字,但它可能500不同,我们可以用%*d替换格式字符串的那部分。 That means sscanf will search for a number (specifically an integer) there, but not assign it to anything. 这意味着sscanf将在那里搜索一个数字(特别是整数),但不会将其分配给任何东西。 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. 如果它找到的不是整数,转换将失败,因此(例如) TIME/DISTANCE ABC/1.234将失败,但TIME/DISTANCE 234/1.l234将成功。

When processing your line then you can use line.find() to check its the right line and to find your data: 在处理您的行时,您可以使用line.find()检查其右侧行并查找您的数据:

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) 编辑:修正了错误(添加4到pos跳过“500 /”部分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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