私はC++を学んでおり、練習するプロジェクトを開発していますが、今では変数(String)をコード内で変更したいと考えています。このように、ユーザーはC++コードを含むファイルを持っています私のプログラムは、そのファイルを読み込むことをしたいとコードに挿入し、このような:C++コードで文字列を変換する
#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;
}
そして、ここでは、ファイルexmapleです:
code "time_t seconds;\n seconds = time (NULL);\n cout << seconds/3600;"
しかし、私は、それはひそかていないプログラムを実行すると文字列をコードに変換しますが、引用符に含まれるものを正確に出力します。
ありがとうございます!
しかし、どのように私は、コードの注入を行うために、coutを行うことなくこれを行うことができます。ありがとう!質問にある私のコードを –
! –