Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading code from a text file
Forum Updated to NodeBB v4.3 + New Features

Reading code from a text file

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 1.0k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rovtar
    wrote on 9 Mar 2020, 09:10 last edited by
    #1

    I want to write some part of my code in a .txt file and then execute it from that file and I just want to know if that's even possible and if it is I'd be thankful for any kind of advice.

    P 1 Reply Last reply 9 Mar 2020, 14:37
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 9 Mar 2020, 09:26 last edited by
      #2

      What is the used language? For compiled languages, you cannot execute a source file... More information is needed to answer you.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      5
      • R Offline
        R Offline
        rovtar
        wrote on 9 Mar 2020, 10:44 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JohanSolo
          wrote on 9 Mar 2020, 10:46 last edited by JohanSolo 3 Sept 2020, 11:36
          #4

          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.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          4
          • R Offline
            R Offline
            rovtar
            wrote on 9 Mar 2020, 14:20 last edited by
            #5

            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();
            }
            
            J 1 Reply Last reply 9 Mar 2020, 14:37
            0
            • J Offline
              J Offline
              JohanSolo
              wrote on 9 Mar 2020, 14:29 last edited by
              #6

              Thanks for the clarification. Now, what are you expecting from "executing" those lines?

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              1 Reply Last reply
              0
              • R rovtar
                9 Mar 2020, 14:20

                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();
                }
                
                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 9 Mar 2020, 14:37 last edited by
                #7

                @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

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                5
                • R rovtar
                  9 Mar 2020, 09:10

                  I want to write some part of my code in a .txt file and then execute it from that file and I just want to know if that's even possible and if it is I'd be thankful for any kind of advice.

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 9 Mar 2020, 14:37 last edited by
                  #8

                  @rovtar

                  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?


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  2
                  • R Offline
                    R Offline
                    rovtar
                    wrote on 9 Mar 2020, 14:59 last edited by
                    #9

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

                    M J 2 Replies Last reply 9 Mar 2020, 15:07
                    0
                    • R rovtar
                      9 Mar 2020, 14:59

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

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 9 Mar 2020, 15:07 last edited by
                      #10

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

                      J 1 Reply Last reply 10 Mar 2020, 07:48
                      3
                      • R rovtar
                        9 Mar 2020, 14:59

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

                        J Offline
                        J Offline
                        JonB
                        wrote on 9 Mar 2020, 17:01 last edited by JonB 3 Sept 2020, 17:02
                        #11

                        @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 ;-)

                        1 Reply Last reply
                        1
                        • M mrjj
                          9 Mar 2020, 15:07

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

                          J Offline
                          J Offline
                          JohanSolo
                          wrote on 10 Mar 2020, 07:48 last edited by
                          #12

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

                          `They did not know it was impossible, so they did it.'
                          -- Mark Twain

                          1 Reply Last reply
                          3

                          1/12

                          9 Mar 2020, 09:10

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved