Hello, 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;
}
And here is a file exmaple:
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.
Thanks!