Jump to content
  • Advertisement
Sign in to follow this  

Char Arrays and system()

This topic is 2642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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";

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: http://dl.dropbox.co...4231/error1.png
Sorry about that. Silly me (facepalm)

Share this post

Link to post
Share on other sites
Use std::string:

std::string command = "title ";
command += title;

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 SetConsoleTitle() and SetConsoleTextAttribute().

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!