Public Group

# 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.

## 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.

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 on other sites
Use std::string:

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

##### Share on other sites
Also. How do I do escape code if I am using a .ini. They dont seem to work.....

##### Share on other sites
What do you want to escape?

##### Share on other sites
Like using '\n' and '\t' and stuff

##### 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.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633271
• Total Posts
3011160
• ### Who's Online (See full list)

There are no registered users currently online

×