Reading code from a file

M

mbrigdan

What I am trying to do is to create a very basic A-life simulator.
What I can't figure out is how to read and execute C++ code from a
file, if I could do this it would be simple to make custom files or
have the main program make them.
Any help would be appreciated.
 
O

Obnoxious User

What I am trying to do is to create a very basic A-life simulator.
What I can't figure out is how to read and execute C++ code from a
file, if I could do this it would be simple to make custom files or
have the main program make them.
Any help would be appreciated.

You mean load external files with c++ code and execute them within
your main application. Then you either need a c++ interpreter,
or you have to compile them first and then execute. I think
you're better of with some other higher language if you
need this kind of functionality.
 
B

BobR

What I am trying to do is to create a very basic A-life simulator.
What I can't figure out is how to read and execute C++ code from a
file, if I could do this it would be simple to make custom files or
have the main program make them.
Any help would be appreciated.

Just compile it, [check output], and run it using 'system()'.

[ untested. just for an idea. ]
std::string comp( "g++ " );
std::string cpplibs( "-lmylib " );
std::string cppfile( "MyFile.cpp " );
std::string Command = "\"" + comp + cppfile + cpplibs + "\"";
system( Command );
// check it here
system( MyFile );

Get the source for some IDE, and examine how they do it.
 
M

mbrigdan

What I am trying to do is to create a very basic A-life simulator.
What I can't figure out is how to read and execute C++ code from a
file, if I could do this it would be simple to make custom files or
have the main program make them.
Any help would be appreciated.

Just compile it, [check output], and run it using 'system()'.

[ untested. just for an idea. ]
std::string comp( "g++ " );
std::string cpplibs( "-lmylib " );
std::string cppfile( "MyFile.cpp " );
std::string Command = "\"" + comp + cppfile + cpplibs + "\"";
system( Command );
// check it here
system( MyFile );

Get the source for some IDE, and examine how they do it.

Can you explain that a bit more? It might be just what I need.
Just to clear up what I need it for:

I need it the main program to dynamically (At runtime) genarate code.
The only way I can see for this to be done is to (have the program)
write it to a file then load it back at a later time.

I need to be able to create and run a large number of files without
having to code in a loading mechanism for each one.
 
B

BobR

Just compile it, [check output], and run it using 'system()'.
[ untested. just for an idea. ]
std::string comp( "g++ " );
std::string cpplibs( "-lmylib " );
std::string cppfile( "MyFile.cpp " );
std::string Command = "\"" + comp + cppfile + cpplibs + "\"";
system( Command.c_str() );
// check it here
system( MyFile );
Get the source for some IDE, and examine how they do it.

Can you explain that a bit more? It might be just what I need.
Just to clear up what I need it for:

I need it the main program to dynamically (At runtime) genarate code.
The only way I can see for this to be done is to (have the program)
write it to a file then load it back at a later time.

Always attack the problem in small steps. First write a program that writes
a simple program.

// - includes here -
#include <iostream>
#include <fstream>

int main(){
std::cout<<"Hello world!"<<std::endl;

std::eek:fstream out( "mymain.cpp" );
if( not out.is_open() ){
return EXIT_FAILURE;
} // if()

out<<"\n\n// - includes here -\n"
"#include <iostream>\n\n"
"int main(){\n"
" std::cout<<\"Hello world!\"<<std::endl;\n"
" return 0;\n"
" } // main()\n"<<std::endl;

return 0;
} // main()

Compile and run that. Then, with your compiler, compile and run "mymain.cpp"
to test it.
Then you can add in (just before 'return 0;' in main() ) that earlier code
(above), modify the strings to fit your compiler, and try it. (no
guarantees!! <G>).
Unfortunately, what goes into the paranthesis in a 'system()' call is not
really on topic in this NG. Other than that, post back if you have trouble.
I need to be able to create and run a large number of files without
having to code in a loading mechanism for each one.

You might do something like:
// .....
std::vector<std::string> programs;
std::string pgm1( "\n\n// - includes here -\n"
"#include <iostream>\n\n"
"int main(){\n"
" std::cout<<\"Hello world!\"<<std::endl;\n"
" return 0;\n"
" } // main()\n"
);
programs.push_back( pgm1 );
// .....
// ofstream out( .... );
out<<programs.at( 0 );
// .....
 
J

Jim Langston

What I am trying to do is to create a very basic A-life simulator.
What I can't figure out is how to read and execute C++ code from a
file, if I could do this it would be simple to make custom files or
have the main program make them.
Any help would be appreciated.

Just compile it, [check output], and run it using 'system()'.

[ untested. just for an idea. ]
std::string comp( "g++ " );
std::string cpplibs( "-lmylib " );
std::string cppfile( "MyFile.cpp " );
std::string Command = "\"" + comp + cppfile + cpplibs + "\"";
system( Command );
// check it here
system( MyFile );

Get the source for some IDE, and examine how they do it.

Can you explain that a bit more? It might be just what I need.
Just to clear up what I need it for:

I need it the main program to dynamically (At runtime) genarate code.
The only way I can see for this to be done is to (have the program)
write it to a file then load it back at a later time.

I need to be able to create and run a large number of files without
having to code in a loading mechanism for each one.

Another option is to compile the source into some type of run time library
that your program can then load and execute some function.

However, an interpreter is probably better. You can probably find some C++
interpreter on the net I would go with a an interpreted langauge, however,
such as maybe python.
 
M

mbrigdan

Just compile it, [check output], and run it using 'system()'.
[ untested. just for an idea. ]
std::string comp( "g++ " );
std::string cpplibs( "-lmylib " );
std::string cppfile( "MyFile.cpp " );
std::string Command = "\"" + comp + cppfile + cpplibs + "\"";
system( Command.c_str() );
// check it here
system( MyFile );
Get the source for some IDE, and examine how they do it.
Can you explain that a bit more? It might be just what I need.
Just to clear up what I need it for:
I need it the main program to dynamically (At runtime) genarate code.
The only way I can see for this to be done is to (have the program)
write it to a file then load it back at a later time.

Always attack the problem in small steps. First write a program that writes
a simple program.

// - includes here -
#include <iostream>
#include <fstream>

int main(){
std::cout<<"Hello world!"<<std::endl;

std::eek:fstream out( "mymain.cpp" );
if( not out.is_open() ){
return EXIT_FAILURE;
} // if()

out<<"\n\n// - includes here -\n"
"#include <iostream>\n\n"
"int main(){\n"
" std::cout<<\"Hello world!\"<<std::endl;\n"
" return 0;\n"
" } // main()\n"<<std::endl;

return 0;
} // main()

Compile and run that. Then, with your compiler, compile and run "mymain.cpp"
to test it.
Then you can add in (just before 'return 0;' in main() ) that earlier code
(above), modify the strings to fit your compiler, and try it. (no
guarantees!! <G>).
Unfortunately, what goes into the paranthesis in a 'system()' call is not
really on topic in this NG. Other than that, post back if you have trouble.


I need to be able to create and run a large number of files without
having to code in a loading mechanism for each one.

You might do something like:
// .....
std::vector<std::string> programs;
std::string pgm1( "\n\n// - includes here -\n"
"#include <iostream>\n\n"
"int main(){\n"
" std::cout<<\"Hello world!\"<<std::endl;\n"
" return 0;\n"
" } // main()\n"
);
programs.push_back( pgm1 );
// .....
// ofstream out( .... );
out<<programs.at( 0 );
// .....

Its not exactly what I'm looking for (I more need a way to execute the
file as a function)
but I guess I'll have to make do.

It seems to work but I get one error when I run it (Not in my
compiler)

Code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
cout<<"Hello world!"<<endl;

ofstream out( "mymain.cpp" );
if( not out.is_open() ){
return EXIT_FAILURE;
} // if()

out<<"\n\n// - includes here -\n"
"#include <iostream>\n\n"
"using namespace std;\n"
"int main(){\n"
" cout<<\"Hello world!\"<<endl;\n"
" return 0;\n"
" } // main()\n"<<endl;
string comp( "F:\Dev-Cpp5\\" );
string cppfile( "MyFile.cpp " );
string Command = comp + cppfile;
const char* myCommand = Command.c_str();
int result = system( myCommand );
cout << cppfile << " Outputs " << result << endl;
system ("PAUSE");
return 0;
}


The result of this is:

Hello world!
The system cannot find the path specified
MyFile.cpp Outputs 1
Press any key to continue...
 
B

BobR

Its not exactly what I'm looking for (I more need a way to execute the
file as a function)
but I guess I'll have to make do.

It seems to work but I get one error when I run it (Not in my
compiler)

Code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
cout<<"Hello world!"<<endl;

ofstream out( "mymain.cpp" );
if( not out.is_open() ){
return EXIT_FAILURE;
} // if()

out<<"\n\n// - includes here -\n"
"#include <iostream>\n\n"
"using namespace std;\n"
"int main(){\n"
" cout<<\"Hello world!\"<<endl;\n"
" return 0;\n"
" } // main()\n"<<endl;
string comp( "F:\Dev-Cpp5\\" );
string cppfile( "MyFile.cpp " );

// from above: ofstream out( "mymain.cpp" );
File Names should match.
Hint:
string cppfile( "MyFile.cpp " );
ofstream out( cppfile.c_str() );
// .....
// string cppfile( "MyFile.cpp " );

string Command = comp + cppfile;
const char* myCommand = Command.c_str();
int result = system( myCommand );

Or, just do:
int result = system( Command.c_str() );
 
D

Dennis Jones

Jim Langston said:
I need to be able to create and run a large number of files without
having to code in a loading mechanism for each one.

Another option is to compile the source into some type of run time library
that your program can then load and execute some function.

However, an interpreter is probably better. You can probably find some
C++ interpreter on the net[/QUOTE]

Definitely...these guys have a C/C++ interpreter:

http://www.softintegration.com/

The standard edition is free. If you want the interpreter built into your
app, you will have to get the embedded version (which is, apparently, not
free), though the website does not list the price.

- Dennis
 
J

Jim Langston

Dennis Jones said:
Another option is to compile the source into some type of run time
library that your program can then load and execute some function.

However, an interpreter is probably better. You can probably find some
C++ interpreter on the net

Definitely...these guys have a C/C++ interpreter:

http://www.softintegration.com/

The standard edition is free. If you want the interpreter built into your
app, you will have to get the embedded version (which is, apparently, not
free), though the website does not list the price.[/QUOTE]

LUA is also an option. It is an interpreted language that you can hook into
from C or C++ or other languages. It is not C code it interprets, however,
but LUA code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top