美文网首页
用\0分割字符串保存到vector

用\0分割字符串保存到vector

作者: 菜菜子MJ | 来源:发表于2019-11-13 12:26 被阅读0次

    把\0替换成其他字符,比如空格

    //此处读取命令行
        std::fstream fs("/proc/self/cmdline");
        std::stringstream ss;
        ss << fs.rdbuf();
        std::string cmd = ss.str();
        LOG(INFO) << "cmd:" << cmd << std::endl;
        int pos_0 = cmd.find('\0');
        while (pos_0 != -1) {
            cmd.replace(pos_0, 1, " ");
            pos_0 = cmd.find('\0');
        }
        LOG(INFO) << "cmd:" << cmd << std::endl;
    
        //     Parse gflags.
        std::fstream fs("/proc/self/cmdline");
        std::stringstream ss;
        ss << fs.rdbuf();
        std::string cmd = ss.str();
        //    char* argv_temp[10000];
        //    int argc = 1;
        //    argv_temp[0] = &cmd[0];
        //    for (int i = 0; i < cmd.length() - 1; i++) {
        //        if (cmd[i] == '\0' && argc < 10) {
        //            if (strncmp(&cmd[i + 1], "--flagfile", 10)) {
        //                continue;
        //            }
        //            argv_temp[argc] = &cmd[i + 1];
        //            std::cout << "i= " << i << " argc=" << argc <<
        //            argv_temp[argc]; argc++;
        //        }
        //    }
    
        LOG(INFO) << "saved cmd:" << cmd << std::endl;
        int pos_0 = cmd.find('\0');
        while (pos_0 != -1) {
            cmd.replace(pos_0, 1, " ");
            pos_0 = cmd.find('\0');
        }
        LOG(INFO) << "original cmd:" << cmd << std::endl;
    
        // use C++11
        //    std::regex re_zero(" ");
        //    std::vector<std::string> cmd_vector(
        //            std::sregex_token_iterator(cmd.begin(), cmd.end(), re_zero,
        //            -1), std::sregex_token_iterator());
    
        // use string
        //    std::string delimiters = "\0";
        //    std::vector<std::string> cmd_vector;
        //    auto lastPos = cmd.find_first_not_of(delimiters, 0);
        //    auto pos = cmd.find_first_of(delimiters, lastPos);
        //    while (std::string::npos != pos || std::string::npos != lastPos) {
        //        cmd_vector.emplace_back(cmd.substr(lastPos, pos - lastPos));
        //        lastPos = cmd.find_first_not_of(delimiters, pos);
        //        pos = cmd.find_first_of(delimiters, lastPos);
        //    }
    
        // use strtok
        std::vector<std::string> cmd_vector;
        char cmd_char[cmd.length() + 1];
        int cmd_i = 0;
        for (cmd_i = 0; cmd_i < cmd.length(); cmd_i++) {
            cmd_char[cmd_i] = cmd[cmd_i];
        }
        cmd_char[cmd_i] = '\0';
        char* delim = (char*)" ";
        char* p = nullptr;
        std::string cmd_item = std::strtok(cmd_char, delim);
        cmd_vector.push_back(cmd_item);
        while ((p = std::strtok(nullptr, delim))) {
            std::string cmd_item_p = p;
            cmd_vector.push_back(cmd_item_p);
        }
    
        int argc = cmd_vector.size();
        char** argv = (char**)malloc(sizeof(char*) * argc);
        //    char argv[100][200];
        for (auto i = 0; i < argc; i++) {
            int len = cmd_vector[i].length();
            argv[i] = (char*)malloc(sizeof(char) * (len + 1));
            cmd_vector[i].copy(argv[i], len, 0);
            argv[i][len] = '\0';
            LOG(INFO) << "cmd item:" << argv[i] << std::endl;
        }
    

    相关文章

      网友评论

          本文标题:用\0分割字符串保存到vector

          本文链接:https://www.haomeiwen.com/subject/qgehictx.html