Can anyone let me know how I could include a variable within a system command?
(This is a win32 project and not an application.)

I am using: (some code taken out)

system(("set path=%path%;"+user).c_str());
system(("set CLASSPATH=.;"+usertwo).c_str());
	
system(("java -Djava.security.manager -Djava.security.policy="+user+"\examplepolicy
Xsys").c_str());

Thanks in advance!!!

Recommended Answers

You have the option of string.append(), for the end of a string; and string.insert() to insert characters at a known position, in a string.

string setpath = "set setpath=%path%;";
setpath.append(user);
system(setpath.c_str());

Or read The World of DOS - Creating Batch Files, that might help.

Jump to Post

I hope you realize that changing environment variables in a program is not permanent and good only as long as the program is running. Just like *nix, any changes made will disappear when the program stops.

Jump to Post

Would it be: str.insert(53,setjava); For
[..]

53 being the 53rd character -Sorry, I am beginner at this!

Why don't you just try it and see? You might want to put setjava in double quotes "setjava".

If you have no experience in C++ (which I'm assuming), you …

Jump to Post

You can use string.find(), so you don't have to count long strings.

Jump to Post

All 15 Replies

You have the option of string.append(), for the end of a string; and string.insert() to insert characters at a known position, in a string.

string setpath = "set setpath=%path%;";
setpath.append(user);
system(setpath.c_str());

Or read The World of DOS - Creating Batch Files, that might help.

I hope you realize that changing environment variables in a program is not permanent and good only as long as the program is running. Just like *nix, any changes made will disappear when the program stops.

MosaicFuneral - Thanks very much for that! Will try get my head around str.insert();

Would it be: str.insert(53,setjava); For

system(("java -Djava.security.manager -Djava.security.policy="+user+"\examplepolicy
Xsys");

53 being the 53rd character -Sorry, I am beginner at this!

Ancient Dragon - Thanks for the advice. This is intended to be a listener project. Which will eventually listener for traffic on port 6666. I did not want to create a batch file because I did not want anyone to view the source and change certain aspects. A .bat file is something to think about though!! :-/

Would it be: str.insert(53,setjava); For
[..]

53 being the 53rd character -Sorry, I am beginner at this!

Why don't you just try it and see? You might want to put setjava in double quotes "setjava".

If you have no experience in C++ (which I'm assuming), you might be better of with Ancient Dragon's advice to make a batch file.

Why don't you just try it and see? You might want to put setjava in double quotes "setjava".

If you have no experience in C++ (which I'm assuming), you might be better of with Ancient Dragon's advice to make a batch file.

Very True - but to be honest, I do prefer C++ evan though its new to me.

Thanks for the advice anyway ;)

You have the option of string.append(), for the end of a string; and string.insert() to insert characters at a known position, in a string.

string setpath = "set setpath=%path%;";
setpath.append(user);
system(setpath.c_str());

Or read The World of DOS - Creating Batch Files, that might help.

This is not working, but I'm sure its just me??

string user;

cout << "1. Enter FULL PATH to directory: ";
	cin >> entry;

user = entry;

string setpath = "set setpath=%path%;";
	setpath.append(user);
	system(setpath.c_str());

	string setclass = "set CLASSPATH=.;";
	setclass.append(user);
	system(setclass.c_str());

	string setjava = "java -Djava.security.manager -Djava.security.policy=\examplepolicy Xsys";
	setjava.insert(52,user);
	system(setjava.c_str());

Insert should be after "policy="

Any ideas???

You can use string.find(), so you don't have to count long strings.

You can use string.find(), so you don't have to count long strings.

Thanks - but I'll stick with setjava.insert(53,user); for now.

I take it that the first Character from a string is "0" and the seond a "1" when using setjava.insert(53,user);

Correct, "most" things while counting, in C++, start at 0.

A reference of string, if you need:
http://www.cplusplus.com/reference/string/string/

Thanks again - do you know if there is something similar to "echo on" in c++?? I would like to know what’s going on in the background. I am able to compile with no probs, but the .exe is not working as expected.

A better soultion might be:
Have your C++ prog. create a .bat file, run it, then delete it.

A better soultion might be:
Have your C++ prog. create a .bat file, run it, then delete it.

OK - let me know if i wrong, but you suggest:

  • Have the c++ file create a .bat file with the required commands (dynamically). Then once the file has completed its function, delete it - a bit like a temp file? Mmmmm that could be an idea!

OK - let me know if i wrong, but you suggest:

  • Have the c++ file create a .bat file with the required commands (dynamically). Then once the file has completed its function, delete it - a bit like a temp file? Mmmmm that could be an idea!

Forgot to say - How?

Just search for "C++ file i/o",and you'll get plenty of tutorials.
If you have any problems, with that, then start a new thread about it.

And I should ask, "What are you doing; that cannot be done in C++, or with the O/S's API?"

Just search for "C++ file i/o",and you'll get plenty of tutorials.
If you have any problems, with that, then start a new thread about it.

And I should ask, "What are you doing; that cannot be done in C++, or with the O/S's API?"

Thanks for your guidance!

I am trying to create a listening application which will listen and wait for communications via port 6666. This involves setting a CLASSPATH and calling the JAVA KEYTOOL.

I'm very sure it can be done via C++ so I'm going to crack on with it.

:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.