用C ++代码转换字符串 [英] Convert a String in C++ Code

查看:50
本文介绍了用C ++代码转换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++并开发一个项目来练习,但是现在我想在代码中转换一个变量(字符串),这样,用户有一个包含C ++代码的文件,但是我希望我的程序读取该文件文件并将其插入代码,如下所示:

I'm learning C++ and developing a project to practice, but now i want to turn a variable(String) in code, like this, the user have a file that contains C++ code, but i want that my program reads that file and insert it into the code, like this:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{
    ifstream file(argv[ 1 ]);
    if (!file.good()) {
       cout << "File " << argv[1] << " does not exist.\n";
      return 0;
    }
    string linha;
    while (!file.eof())
    {
    getline(file, linha);
    if (linha.find("code") != string::npos)
       {
          size_t idx = linha.find("\""); //find the first quote on the line
          while ( idx != string::npos ) {
             size_t idx_end = linha.find("\"",idx+1); //end of quote
             string quotes;
             quotes.assign(linha,idx,idx_end-idx+1);
             // do not print the start and end " strings
             cout << quotes.substr(1,quotes.length()-2) << endl;
             //check for another quote on the same line
             idx = linha.find("\"",idx_end+1);
             } 
       }
    }
  return 0;
}

这是一个文件示例:

code "time_t seconds;\n seconds = time (NULL);\n cout << seconds/3600;"

但是,当我运行该程序时,它不会将字符串隐藏到代码中,但它会完全打印出引号中的内容.

But when i run the program it don't covert the string into code, but it prints exactly what is in the quotes.

谢谢!

推荐答案

您正在执行指令吗?所以很明显它被显示了.

You're doing cout right? So obviously it gets displayed.

也许您要尝试的是在正在运行的进程中进行一些代码注入,例如http://www.codeproject.com/KB/DLL/code_injection.aspx

Maybe what you are trying to do is some code injection in a running process like this http://www.codeproject.com/KB/DLL/code_injection.aspx

这篇关于用C ++代码转换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆