Nathan Campos
Nathan Campos

Reputation: 29497

Convert a String in C++ Code

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!

Upvotes: 0

Views: 3817

Answers (6)

As other have noted that c++ is generally a compiled language, and simply supplies no native support for this.

Two possible solutions to the question as you ask it:

  • Find and use a c++ interpreter. There are several StackOverflow questions on the matter, and at least two such pieces of software exist. I prefer cint.
  • Invoke a c++ compiler on the code to generate a dynamic library, which you subsequently load and call. This won't be exactly like your example, but case the user must supply "callable" code (i.e. one or more functions).

Possible solution to the question you may have meant:

  • Use an extension language such a python, tcl or lua.

Upvotes: 0

djna
djna

Reputation: 55907

If the objective is to execute some externally provided script I'd suggest that the script be specified as one of the commonly used scripting languages. We did this with perl many years ago. This link text dscribes how.

While dynamically compiling and linking C++ code is technically possible it's quite tricky and the result may not be too robust - consider how lilely it may be that the "script" author misuses a pointer and trashes something important.

Scripting languages tend to be more tractable than C++ for less technical authors

Upvotes: 0

user147714
user147714

Reputation: 133

You can't do what you want in C++. To evaluate command line arguments, you'd need to embed a scripting language into your program (Python would seem like a good example - it's not hard). The the string argument can be evaluated as Python code.

Upvotes: 1

user108127
user108127

Reputation: 486

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

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

Upvotes: 3

EFraim
EFraim

Reputation: 13028

What do you want is to actually evaluate the string at runtime. This is not directly supported in C++, or in other non-interpretted/jitted languages.

Upvotes: 1

ThibThib
ThibThib

Reputation: 8210

C++ is a compiled language, not an interpreted language.

So, it is not possible for a program to read C++ code on-the-fly and execute it, since this code requires to be compiled.

Upvotes: 5

Related Questions