Я изучаю C ++ и разрабатываю проект на практике, но теперь я хочу превратить переменную (String) в код, например, у пользователя есть файл, содержащий код 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;"
Но когда я запускаю программу, она не преобразует строку в код, а печатает именно то, что находится в кавычках.
Спасибо!