Sign in to follow this  
spacekid434

Char Arrays and system()

Recommended Posts

Hey everyone.

I have been working again on a little console formatting script thingy that I made a few years ago and I am support to set certain options for the program in a .ini file. I have the ini read working but now I am having trouble with using the char array I use as the buffer for GetPrivateProfileStringA().

Here is the code with the problem
[code] char initCol[125];
char title[125];
char firstLine[125];
char secLine[125];
char soundFile[125];

GetPrivateProfileStringA("settings", "initCol", "E", initCol, 125, "data\\config.ini");
GetPrivateProfileStringA("settings", "title", "Untitled CMD", title, 125, "data\\config.ini");
GetPrivateProfileStringA("settings", "firstLine", "", firstLine, 125, "data\\config.ini");
GetPrivateProfileStringA("settings", "secLine", "", secLine, 125, "data\\config.ini");
GetPrivateProfileStringA("settings", "soundFile", "", soundFile, 125, "data\\config.ini");

system("title " + *title); //compile error
system("color " + *initCol);//compile error
cout << firstLine;
cout << secLine;

if(!PlaySoundA(soundFile, NULL, SND_ASYNC))
cout << "FAILED\n\n";[/code]


The problem is that I need to prefix 'title' and 'initCol' with "title " and "color " respectively but I cant work out how to do it.


please help.

EDIT: Sorry guys, it isn't a compile error, it is an error when those lines of code are executed. Link to the error here: [url="http://dl.dropbox.com/u/21274231/error1.png"]http://dl.dropbox.co...4231/error1.png[/url]
Sorry about that. Silly me (facepalm)

Share this post


Link to post
Share on other sites
Use std::string:
[quote]
std::string command = "title ";
command += title;
system(command.c_str());
[/quote]

Note that using system() like this is dangerous, and could allow for executing code that you didn't expect. In addition, it is expensive to spawn a sub-process just to change these properties.

An alternative is to use the Win32 api functions [url="http://msdn.microsoft.com/en-us/library/ms686050(v=vs.85).aspx"]SetConsoleTitle[/url]() and [url="http://msdn.microsoft.com/en-us/library/ms686047(v=vs.85).aspx"]SetConsoleTextAttribute[/url]().

Share this post


Link to post
Share on other sites
Escape sequences are for string literals in source files. If you are loading data from an external data file then the \ character is just another character, not the beginning of an escape sequence. If you want tabs and newlines in your external data ... you just use actual tabs and newlines. Whether you can embed newlines into your config.ini file is something you'll have to research.

You can, of course, write your own routine that takes a string you have loaded and replaces escape sequences with the corresponding character literalys, but you'll need to write that yourself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this