c_string vs string

Pages: 12
Hi,

I am using the concept of pipes in a c++ program, and the software which gets the info through pipe needs a char* script.
If I understood well, that is what is called a c-string (null-terminated sequence of characters).
I encounter the same problem while using system commands inside my program (see the example below).

It makes me struggle a lot with the difference between c_string and string: I can't easily concatenate two c_strings and I am forced to use some kind of trick like this to convert them:

1
2
3
4
5
6
7
string const fileName(argv[1]); // e.g. C:/Users/Robin/Desktop/out.txt
char *cstr_sys(new char [fileName.size()+1]);
strcpy (cstr_sys, fileName.c_str());

system(cstr_sys);

delete[] cstr_sys;


Would anybody know any easier way to play with these things?
Thank you a lot in advance



Sky


PS: I already tried to use <vector>, without success... It would have been amazing though, since the allocation makes it a pain the butt
Last edited on
In other words, does anybody know if any easy and suitable conversion function from std::string to char * exists?
It is the member function c_str. Why can not you simply use system( fileName.c_str() ); ?
Indeed, that works for this occurrence, and I was sure I had already tried it, sorry...
Though it works only for this problem, I still can't concatenate them easily.
For [other] example, to write "T = 100" in the script I send through the pipe, I use this complicated translation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*  Input parameters  */

const double temp(100); // in [K]

/*  C-string Concatenation  */

	string tempInString=static_cast<ostringstream*>( &(ostringstream()<<temp) )->str();
	string str_temp="T = "+tempInString;
	char *cstr_temp(new char [str_temp.size()+1]);
	strcpy (cstr_temp, str_temp.c_str());

/*  First script, that will be sent through pipe1  */

char* script1[] = 
{
	"reset",
	cstr_temp,
	"SiO2(aq) = 1 umolal",
	"react 5000 g Quartz",
	"kinetic Quartz rate_con = 2.e-15 surface = 1000",
	"go",
	""
};


... Any idea?
Thank you very much.


Sky
1
2
3
4
5
6
7
const double temp(100); // in [K]
stringstream ss;
ss << "T = " << temp;
string str=ss.str();
const char* cstr_temp=str.c_str();

const char* script1[] = 
Thanks for your help Athar, but I can't afford to change the type of the script, it has to be char* script1[] if I want the pipes to work... :s
Then use const_cast. This is fine as long as your pipe thing does not write to the C strings. Which it couldn't do anyway because it contains string literals.
After writing char* script1 = const_cast<char*>(script1[]);, I get the error
Initialization with '{...}' expected for aggregate object
on const_cast.

And at the compilation appear :

Error 4 error C2059: syntax error : ']' pipecmd.cpp 70 Error 5 error C2143: syntax error : missing '(' before ']' pipecmd.cpp 70

But it won't work if use const_cast<char*>(script1) either.....
Last edited on
Of course, you have an array of C strings, not a C string. It's const_cast<char**>(script1).
Oh! My bad... I get to use c_strings but I'm still not very comfortable with them...
Unfortunately it still doesn't work... I know get Error 4 error C2440: 'initializing' : cannot convert from 'char **' to 'char *[]' pipecmd.cpp 70
What is "it"?
The compilation of the program...
Last edited on
I was more interested in the line of code that triggered the compilation error.
I was wondering... :)
Here it is :
char* script1[] = const_cast<char**>(const_script1);
You can't create a second variable with the same name.
And you casted to char**, so that's what you get. You can't assign a pointer to an array.
Last edited on
The 1st variable is named const_script1 and the second one script1...
I don't get the second statement you made, sorry... Here is this whole part of the code, with cstr_time and cstr_temp the variables set as said earlier in this conversation (Apr 20, 2012 at 12:37pm)

1
2
3
4
5
6
7
8
9
10
11
12
const char* cscript1[] = 
{
	"reset",
	cstr_time,
	cstr_temp,
	"SiO2(aq) = 1 umolal",
	"react 5000 g Quartz",
	"kinetic Quartz rate_con = 2.e-15 surface = 1000",
	"go",
	""
}; 
char* script1[] = const_cast<char**>(cscript1);


But I still don't get why this const_cast doesn't work... :s
const_cast works fine, but the initialization does not.
char* script1[] = ... declares an array of char*. You're trying to initialize it with a pointer to a pointer to char. That's not gonna work.
Hi again Athar, sorry for the delay in my replies and thanks for your continuing help

I mean, yes the initialization works : my whole program runs this way already (it is just that the new and deletes are a pain in the butt, and the const_cast could really help me to get rid of them), and this syntax is actually what I started with (p352 of http://www.gwb.com/pdf/GWB9/GWBreference.pdf).


Well, this is how it should look like:
char** script1 = const_cast<char**>(cscript1);
My script1 cannot be of type char**, it has to be a char*, or else the pipe with GWB, initiated by themselves (see previous weblink), doesn't work...
Pages: 12