Unsolved Reading code from a text file
-
What is the used language? For compiled languages, you cannot execute a source file... More information is needed to answer you.
-
I'm writing in c++ and I never intended to execute a source file but rather read from it and put it in a string and then execute it with QProcess. I don't know how exactly it works so I'm not sure it's even possible to do it like that so I'm asking for advice. If there is any other information still necessary I'll gladly provide it to the best of my abilities.
-
What's the language used in the text file?
QProcess
won't help you to magically run random language statements. Could you give us an example of what will be contained in your text file?Edit: I've used the precedessor of Cling, which was called CINT, but I don't think that's what you want.
-
Everything is in c++ and all I want is to include the code that I'd be writing in the file as if it were written in the qt creator editor(i don't know what exactly it's called). Here's a part of the code that I want to read from a .txt file. The names of functions and variables are in my native language but that shouldn't matter.
void Simulacija::titForTat(){ if(i<15){ if(i==0) odl=false; else if(prej_od) odl=true; else odl=false; tockovanje(); i++; prejsnjaOdl(); rezultat(); } else konec(); }
-
Thanks for the clarification. Now, what are you expecting from "executing" those lines?
-
@rovtar You can't execute C++ code directly. You would need an interpreter (just like Python interpreter to execute Python code). But I'm not aware of any C++ interpreter. C++ is a compiled language, that means the C++ source code is compiled into an executable which is then executed.
What you want to do sounds like scripting. Take a look at https://doc.qt.io/qt-5/topics-scripting.html
-
What's the reason for this? You could also write the code in a text editor, save as *.cpp and include this file in your project? Why you want an external text file with code?
-
@jsulm Roger that. Just wanted to make sure about that. Thank you.
@JohanSolo Sorry for wasting your time.
@Pl45m4 My professor gave me an assignment and he specified to do it like this. I'm gonna talk to him tomorrow. -
@rovtar
Hi
Its not possible in any easy way to run c++ code from a text file since
it needs to be compiled to run.
However, using Lua / python or likely the most easy to get going
https://doc.qt.io/qt-5/qjsengine.html#details
You could just load javascript from the text file and execute that with very little coding and
it does provide easy ways of integration if the scripts need access to internal data the app has. -
@rovtar said in Reading code from a text file:
@Pl45m4 My professor gave me an assignment and he specified to do it like this. I'm gonna talk to him tomorrow.
One of you two is mistaken; let us know who ;-)
-
@mrjj said in Reading code from a text file:
Its not possible in any easy way to run c++ code from a text file since
it needs to be compiled to run.For completeness, as said in one of my previous posts, at least there is cling and there was cint, which are C++ interpretors. I've used cint almost 10 years ago, but it was embedded in the ROOT data analysis framework, and I've not idea on if and how to use it in stand alone mode. It offered a console in which you could type C++ statements, which were (I've got no idea how) "executed", you could also execute C++ "scripts".
Anyway this seems much beyond an assignment.