0
How to execute code from string in c++
-
6 Réponses
+ 2
Can you provide more details about it and also some examples.
+ 2
hussein Unfortunately C++ does not have an eval function like JS and as such it is not directly possible for one to execute strings as valid code.
But here is my workaround,(though I've not yet tried it)
1.Write some code and store it in a string.(Use raw strings preferably)
2.Write the string to a temporary file
3.Use system() to invoke the compiler and compile your code
4.Still use system() to execute the program and afterwards, to delete all the temporary files created.
+ 1
Compile and run
0
I want to execute a string like
string s = "cout << 4";
but in string
i want to execute s
how to?
0
It worked lol
0
C++ is compiled to binary so you can't write source code while executing, but there are two alternative ways:
1.write your string to a file and compile it then open the target. But it dome times you don't have compiler in that device and also compiling may take too long.
2.if your available instructions of code are limited you can use some IF or SWITCH code in order to make a function do some thing according to a string.