473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading code from a file

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.

Aug 21 '07 #1
11 1657
On Tue, 21 Aug 2007 13:19:22 -0700, mbrigdan wrote:
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.

--
Obnoxious User
Aug 21 '07 #2

<mb******@gmail.comwrote in message...
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.

--
Bob R
POVrookie
Aug 21 '07 #3
On Aug 21, 3:39 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
<mbrig...@gmail.comwrote in message...
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.

--
Bob R
POVrookie
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.

Aug 21 '07 #4

<mb******@gmail.comwrote in message...
On Aug 21, 3:39 pm, "BobR" <removeBadB...@worldnet.att.netwrote:

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::ofstream 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::stringprograms;
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 );
// .....

--
Bob R
POVrookie
Aug 22 '07 #5
<mb******@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
On Aug 21, 3:39 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
><mbrig...@gmail.comwrote in message...
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.

--
Bob R
POVrookie

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.
Aug 22 '07 #6
On Aug 21, 5:59 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
<mbrig...@gmail.comwrote in message...
On Aug 21, 3:39 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
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::ofstream 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::stringprograms;
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 );
// .....

--
Bob R
POVrookie
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...

Aug 22 '07 #7

<mb******@gmail.comwrote in message...
>
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() );
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...
--
Bob R
POVrookie
Aug 22 '07 #8

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:ZF*************@newsfe12.lga...
<mb******@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
>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
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
Aug 22 '07 #9
"Dennis Jones" <no****@nospam.comwrote in message
news:q2Oyi.5161$jy6.237@trnddc01...
>
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:ZF*************@newsfe12.lga...
><mb******@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegr oups.com...
>>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

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.
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.
Aug 22 '07 #10
On Aug 21, 9:41 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Dennis Jones" <nos...@nospam.comwrote in message

news:q2Oyi.5161$jy6.237@trnddc01...


"Jim Langston" <tazmas...@rocketmail.comwrote in message
news:ZF*************@newsfe12.lga...
<mbrig...@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegr oups.com...
>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
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.

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.
I'm actually just going to try what
http://groups.google.ca/group/comp.l...5ade62e4d82509
suggests but its taking awhile. I'll just put all the functions in my
code once I figure out how to get it to parse the text properly.

Aug 22 '07 #11
<mb******@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
On Aug 21, 9:41 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Dennis Jones" <nos...@nospam.comwrote in message

news:q2Oyi.5161$jy6.237@trnddc01...


"Jim Langston" <tazmas...@rocketmail.comwrote in message
news:ZF*************@newsfe12.lga...
<mbrig...@gmail.comwrote in message
news:11**********************@q3g2000prf.googleg roups.com...
>>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
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.

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.

I'm actually just going to try what
http://groups.google.ca/group/comp.l...5ade62e4d82509
suggests but its taking awhile. I'll just put all the functions in my
code once I figure out how to get it to parse the text properly.
Just calling functions by name is relatively simple, especially if using
boost.

Executing arbitrary code is much more difficult. It depends on what you are
trying to accomplish.
Aug 23 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.