C++ 查找单词+;后面有十个字母

C++ 查找单词+;后面有十个字母,c++,file,text,find,C++,File,Text,Find,首先,我想说我仍在学习,有些人可能会认为我的代码看起来很糟糕,但事情就是这样 所以我有这个文本文件,我们可以称之为example.txt example.txt中的一行可以如下所示: randomstuffhereitem=1234randomstuffhere bool get_price(std::string s, std::string & rest, int & value) { int pos = 0; //To track a position insid

首先,我想说我仍在学习,有些人可能会认为我的代码看起来很糟糕,但事情就是这样

所以我有这个文本文件,我们可以称之为example.txt

example.txt中的一行可以如下所示:

randomstuffhereitem=1234randomstuffhere
bool get_price(std::string s, std::string & rest, int & value)
{
    int pos = 0; //To track a position inside a string
    do //loop through "item" entries in the string
    {
        pos = s.find("item", pos); //Get an index in the string where "item" is found
        if (pos == s.npos) //No "item" in string
            break;
        pos += 4; //"item" length
        while (pos < s.length() && s[pos] == ' ') ++pos; //Skip spaces between "item" and "="
        if (pos < s.length() && s[pos] == '=') //Next char is "="
        {
            ++pos; //Move forward one char, the "="
            while (pos < s.length() && s[pos] == ' ') ++pos; //Skip spaces between "=" and digits
            const char * value_place = s.c_str() + pos; //The number
            if (*value_place < '0' || *value_place > '9') continue; //we have no number after =
            value = atoi(value_place); //Convert as much digits to a number as possible
            while (pos < s.length() && s[pos] >= '0' && s[pos] <= '9') ++pos; //skip number
            rest = s.substr(pos); //Return the remainder of the string
            return true; //The string matches 
        }
    } while (1);
    return false; //We did not find a match
}
我希望我的程序接收item=旁边的数字,我已经开始使用下面的代码对其进行一些处理

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

    string word;

int main()
{
    ifstream readFile("example.txt", ios::app);
    ofstream outfile("Found_Words.txt", ios::app);
    bool found = false; 

    long int price;
    cout << "Insert a number" << endl;
    cout << "number:";
    cin >> number;
    system("cls");
    outfile << "Here I start:";
    while( readFile >> word )
    {
        if(word == "item=")
它将发现:

hello item= hello
它必须用空间隔开,这也是一个问题

其次,我想在item=旁边找到数字。我希望它能够找到item=1234,请注意1234可以是像6723这样的任意数字

我不想让它找到数字后面的内容,所以当数字停止时,它不会再接收数据。Like item=1234必须是item=1234

            {
            cout <<"The word has been found." << endl;
            outfile << word << "/" << number;
            //outfile.close();
                if(word == "item=")
                {
        outfile << ",";
                }

        found = true;
            }
    }
    outfile << "finishes here" ;
    outfile.close();
    if( found = false){
    cout <<"Not found" << endl;
    }
    system ("pause");
}
{

cout您可以使用如下代码:

randomstuffhereitem=1234randomstuffhere
bool get_price(std::string s, std::string & rest, int & value)
{
    int pos = 0; //To track a position inside a string
    do //loop through "item" entries in the string
    {
        pos = s.find("item", pos); //Get an index in the string where "item" is found
        if (pos == s.npos) //No "item" in string
            break;
        pos += 4; //"item" length
        while (pos < s.length() && s[pos] == ' ') ++pos; //Skip spaces between "item" and "="
        if (pos < s.length() && s[pos] == '=') //Next char is "="
        {
            ++pos; //Move forward one char, the "="
            while (pos < s.length() && s[pos] == ' ') ++pos; //Skip spaces between "=" and digits
            const char * value_place = s.c_str() + pos; //The number
            if (*value_place < '0' || *value_place > '9') continue; //we have no number after =
            value = atoi(value_place); //Convert as much digits to a number as possible
            while (pos < s.length() && s[pos] >= '0' && s[pos] <= '9') ++pos; //skip number
            rest = s.substr(pos); //Return the remainder of the string
            return true; //The string matches 
        }
    } while (1);
    return false; //We did not find a match
}
bool get_price(std::string s,std::string&rest,int&value)
{
int pos=0;//跟踪字符串中的位置
do//循环遍历字符串中的“item”条目
{
pos=s.find(“item”,pos);//在找到“item”的字符串中获取索引
if(pos==s.npos)//字符串中没有“item”
打破
pos+=4;/“项目”长度
while(pos'9')继续;//后面没有数字=
value=atoi(value_place);//将尽可能多的数字转换为数字

虽然(pos='0'&&s[pos]问题是我不知道这些代码的作用。我不知道其中99%是什么。我的代码更简单,可能不好…可能很糟糕…但很容易理解。我也不知道在哪里插入代码。所以我编写了这段代码(不知道这是否是你的想法,但是的…)编辑:我不能在这里粘贴代码…我应该在哪里粘贴代码???我不想回答我自己的问题…如果你想学习编程,你需要试着理解我发布的代码。我已经用每行注释对其进行了注释以简化它。你可以将此代码作为一个单独的函数粘贴(即上面的
int main()
)。然后你像
get_price(word,rements,number)
那样调用它。你的代码无法解决任务,因此你需要更改它。我已经向你展示了一个如何更改它的示例。我认为你不能在评论中发布代码,但你可以编辑问题或将代码上载到pastebin.com并发布链接。