Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从字符数组中提取字符串_C++_Input_Substring_Character - Fatal编程技术网

C++ 从字符数组中提取字符串

C++ 从字符数组中提取字符串,c++,input,substring,character,C++,Input,Substring,Character,我尝试过memcpy/strncpy/std::copy,但它们都不起作用,导致程序崩溃 以下是我需要做的: 例如,我正在尝试解析来自用户输入的参数。 “消息-添加0xAE” 我需要将0xAE部分提取为一个整数,下面是一些伪代码 _输入将保存完整字符串“message-add 0xAE” 编辑:谢谢艾伦 if(strstr(_input,"message -add 0x")){ char* _temp = new char[strlen(_input)-strlen("m

我尝试过memcpy/strncpy/std::copy,但它们都不起作用,导致程序崩溃

以下是我需要做的:

例如,我正在尝试解析来自用户输入的参数。 “消息-添加0xAE”

我需要将0xAE部分提取为一个整数,下面是一些伪代码 _输入将保存完整字符串“message-add 0xAE”

编辑:谢谢艾伦

if(strstr(_input,"message -add 0x")){
            char* _temp = new char[strlen(_input)-strlen("message -add 0x")];
            memcpy(_temp,_input+strlen("message -add 0x"),strlen(_input)-strlen("message -add 0x"));
            int _value = atoi(_temp);
            CLog->out("\n\n %d",_value);
}
您正在寻找:

int value = 0;
char c;
for(int i = strlen("message -add 0x"); c = *(_input + i); i++) {
    value <<= 4;
    if(c > '0' && c <= '9') {
        // A digit
        value += c - '0';
    } else if(c >= 'A' && c < 'G') {
        // Hexadecimal
        value += 10 + c - 'A';
    }
}
int值=0;
字符c;
对于(inti=strlen(“message-add0x”);c=*(\u input+i);i++){

值> P>如果你想使用C++,那么避开C函数和所有讨厌的内存管理。我推荐阅读。它确实是一本学习C++的C++实用的顶级书籍。这里没有C字符串解析例程,这里还有另一个解决方案:

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>


int
main()
{
    std::string const TARGET("message -add 0x");
    char const _input[] = "message -add 0xAE23";

    std::string input(_input);
    std::string::size_type offset = input.find(TARGET);
    if (offset != std::string::npos) {
        std::istringstream iss(input);
        iss.seekg(offset + TARGET.length());
        unsigned long value;
        iss >> std::hex >> value;
        std::cout << "VALUE<" << value << ">" << std::endl;
    }

    return 0;
}
#包括
#包括
#包括
#包括
int
main()
{
字符串常量目标(“消息-添加0x”);
字符常量输入[]=“消息-添加0xAE23”;
std::字符串输入(_输入);
std::string::size\u type offset=input.find(目标);
if(偏移量!=std::string::npos){
std::istringstream iss(输入);
iss.seekg(偏移量+目标长度());
无符号长值;
iss>>标准::十六进制>>值;

std::难道我看不到您尝试将字符串转换为整数的代码部分没有初始化。虽然您不想使用任何第三方代码,因此排除了所有好的选项解析器,但您自己尝试这样做可能会导致许多缺陷。您为什么希望重新发明这个特定的轮子?Parsing选项异常困难。除非你是一名技术高超的开发人员,否则你应该乐于使用第三方代码。@David Hefferman抱歉,如果我听起来有点冒犯,我不是有意冒犯。我要感谢你的时间。其他人可能会说你在浪费时间,但传授知识远比不经意地观望要好得多整晚都在打球。至少在我看来是这样。到目前为止,你已经回答了我所有的问题,所以我想让你知道我有多感激你!继续努力!一点也不冒犯。我只是不确定你处于什么水平,你的目标是什么。你自己继续努力!!它工作不正常。如果我输入消息-添加0x1..它返回203或something.@minitech看起来你忘了将值初始化为0是不起作用的;值没有初始化。当输入为十六进制时,你也将其视为十进制。@CareyGregory:对不起,我现在太习惯VB.NET了。我还以为字母表就在数字之后。我确实正确地阅读了问题,即使我没有完全正确地思考问题e:)@minitech:我对vb.net事件表示哀悼;-)
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>


int
main()
{
    std::string const TARGET("message -add 0x");
    char const _input[] = "message -add 0xAE23";

    std::string input(_input);
    std::string::size_type offset = input.find(TARGET);
    if (offset != std::string::npos) {
        std::istringstream iss(input);
        iss.seekg(offset + TARGET.length());
        unsigned long value;
        iss >> std::hex >> value;
        std::cout << "VALUE<" << value << ">" << std::endl;
    }

    return 0;
}