Declare variables in a generated file

Hello

I've got a question, how is it done to generate a new C++ file with the variables declared in another (main) C++ file?

For example:
I've got a C++ application which will make -couts- in another file.

Pseudo code:

Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;
int main() {
string content, output;

cout << "Choose content to show: " <<endl;
cout<< ">";
cin >> content;

cout << "Choose EXE to generate and output content with: " <<endl;
cout<< ">";
cin >> output;
}


Generated.cpp:
1
2
3
4
5
#include <iostream>

int main() {
cout << content <<endl;
}


Is it possible to do so?
And how do I generate a C++ file from a C++ file?

Thanks for reading,
Niely

Last edited on
And how do I generate a C++ file from a C++ file?
1
2
std::ofstream out("Generated.cpp");
out << "#include <iostream>\n\nint main() {\ncout << " << content << " <<endl;\n}\n"; 
will give you file like in your example
@MiiNiPaa:
Thanks a lot, but still have got two questions regarding that:
1) Won't that be a bit hard if Generated.cpp has a very big source?
2) You generate a 'Generated.cpp' wouldn't that just be a source, and not an executable?
You generate a 'Generated.cpp' wouldn't that just be a source
Didn't you asked for C++ file? If you want to build it too, call compiler with your generated. cpp as a parameter.

Won't that be a bit hard if Generated.cpp has a very big source?
You can have a template saved into file where there are places to place some variables.

1) How do I do that without using the system-function?
I once had an application doing that on my Windows, when I even didn't had a compiler installed.

2) Okay thanks, I'll do that. :)
YOu can simply save variables to file and make other program open and output it.
And what if the other program should also be able to operate somewhere else where they don't have that file?
Embed it in program, write in the instruction: do not remove file, create an executable with big gap in data segment, so you can write your own data by known adress.

There are dozens way to do this. Each has own benefits. Best way is to not do that. Why do you need it anyway?
I need an application (A - main), which configures an application (B - generated).

B works with sockets and should be able to interact with servers.
Which servers (IP address) should be configured using A.

So that the user shouldn't be configuring B the whole time it starts-up, besides that the user should be able to create multiple Bs (which isn't possible if I pre-make B; so it must be generated).
And it shouldn't be user-dependent, in the context that B shouldn't depend on text-files, compiler installed or not,...
Create program B as usual with single predefined IP address. Open executable and find where that IP is stored.
Create another program which will copy that executable and change bytes of that IP address.
Enjoy most AV programs blocking your program.
How can you search, and copy an executable if you don't has it source but only the computer language?

And why would AVs block it?
I've already made applications with sockets which don't get detected by AVs.
How can you search, and copy an executable
You will have it embedded in your program.

And why would AVs block it?
Trying to write random data in the middle of another executable is a common virus behavior.
So, I'll have the other program (B) in a variable in A?

Yeah, that's true.
I'll see if it really gets detected, if it does I'll try to find out how to whitelist it again.
Topic archived. No new replies allowed.