Load code from text

How can i load code from a text file to be executed by the compiler?
Last edited on
Very similar to the answer from your previous question. You may want to check out the tutorials on this site.

1
2
3
4
5
fstream file;
file.open("directory/file.txt",ios::in);
string input;
file >> input; //just like cin
file.close();
Maybe I was not clear but I don't think you understand what I am asking. I know how to load data from a text to a string. The code you've given what that does is load data into a string array, I know that.
What I want is to have the data that was stored in the file after it is loaded to be executed by the compiler or if it can be executed directly from the text without loading it into a string
Last edited on
Are you saying that you want a text file containing C++ code to be read by your program, and then that C++ code to be compiled and executed, all within your running program?
If you're asking what Moschops is guessing, here's my two cents.

For one, there are many different C++ compilers. How you could even attempt to start doing this is dependent on which one you choose.

Second, I really don't think this is a good idea anyways. While I'm not sure what your reasons are, IMO C++ was never designed for this kind of usage and shouldn't be. If you want to have programmable logic on the fly, use some sort of scripting language that can be embedded into your C++ program. For example, Lua scripts can actually be loaded from c-style strings or text files and then be executed by a Lua instance within your own native code.
Last edited on
Something like that. Let me elaborate:
Basically a user would create something an object in the program that would be saved to some external file(in my case a text),whenever the program starts it would load this data from the file and use it to recreate the objects
@Austin J:
I am using Visual Studio 2013, not sure what compiler it uses.
Maybe I'm just asking it wrong I just want to load data from a file to be used within the program.

I'm also asking this done a bit of graphics programming sometime ago involving shaders and i recall that you have to write a separate code for the GPU which is loaded into the program
Ah, that explains a lot more.

For your first statement, this depends a lot on what you mean by "objects", but it seems you're asking about making your program more data driven. If the "object" just needs to have data stored, then it's pretty simple. Just write the data to the text file and then read it later. There are lots of learning materials for doing this, although in the end the file format is up to you.

EDIT: I forgot to mention. If the object needs to store behaviour/logic whatever you want to call it, I'd recommend bringing a scripting language into your project.

For your second statement now.

First off, with Visual Studio 2013, you're using the Visual Studio compiler. Microsoft has their own C++ compiler that they release with the IDE.

Secondly...

Indeed in graphics programming there are actually shader languages that can be compiled at runtime. GLSL ( from OpenGL ) and HLSL ( from Direct3D ).

This gets into a pretty big topic actually. Languages such as GLSL constitute what's known as a "programmable graphics pipeline" . You can actually store GLSL code in a hard-coded string, or in a text file. While I haven't used it, I'd imagine HLSL works this way also. These languages are meant to function as little miniature programs called "shaders" and communicate with your GPU for graphics processing purposes.
Last edited on
Thanks for the reply but you mentioned that there are learning materials for loading data.
Have anywhere that i can start?
Writing data to a file and reading it back again: http://www.cplusplus.com/doc/tutorial/files/

When you've read that, read about serialisation: https://en.wikipedia.org/wiki/Serialization
Just for anybody that might come across this, I found something that I think may work for what I was asking:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct NAME
{
	string fname;
	string lname;
};
Name names[100];

int id;
string name;
string fn, ln;

ifstream input("sample.txt");
int count = 0;

while(input>>id>>fn>>ln)
{
	NAME n = {fn, ln};
	names[count] = n;
	++count;
}

I have a clear understanding of what I now need to do
Topic archived. No new replies allowed.