views:

407

answers:

6

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!

+5  A: 

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.

ThibThib
You are wasting your time with this guy.
anon
Neil, I often think the same, too, but on the other hand some people won't learn things if you won't explain them - and will waste much more of their and others time then.
schnaader
Take a look at his previous posts. Some of us have tried, but I for one have givemn up.
anon
@Neil stop acting like the C++ man and the best, be more friendly with the persons that are trying to learn!
Nathan Campos
@Nathan: You have a fundamental misunderstanding of how C++ (and other imperative languages) works. You have to rework this, and not just discard this on the basis of being a "newbie" in C++.
EFraim
+1  A: 

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.

EFraim
+3  A: 

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

But how i can do this without doing cout, to do a code injection. Thanks!
Nathan Campos
with my code that is in the question!
Nathan Campos
+1  A: 

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.

A: 

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

djna
A: 

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.
dmckee