NEWBIE: how to construct a system call from strings

A

Atropo

Hi all,

Having several strings how do I construct variable to pass to
system():
Lets say the date command

string str = "14/10/08 19:06:09";
strDD = str.substr(0,2);
strMM = str.substr(3,2);
strYY = str.substr(6,2);
strhh = str.substr(9,2);
strmm = str.substr(12,2);
strss = str.substr(15,2);

strT<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strYY<<"."<<strss<< "
2>&1 >/dev/null;"<<endl;

system(strT);

when I compile it throws

rtchk.cpp: In function `int main (int, char **)':
rtchk.cpp:59: no match for `string & << const char[9]'

so i could be a syntax error, but I'm newbie.
if there is a typo, I can't see it.
 
A

Atropo

What's wrong with the answer I gave last time you asked?

Sorry Ian. I though those post were deleted after hours refreshing
the webpage this post never shows..
 
I

Ian Collins

*Please* don't quote signatures.
Sorry Ian. I though those post were deleted after hours refreshing
the webpage this post never shows..

Usenet isn't IM, you have to give messages time to propagate. That
dreadful google interface appears to make things worse.
 
A

Atropo

*Please* don't quote signatures.
Again sorry, but I don't understand what you mean.
Usenet isn't IM, you have to give messages time to propagate.  That
dreadful google interface appears to make things worse.

Yo're absolutely right. but I'm RTFM.

about my issue, you told me on the other similar post to use
ostringstream. I'm reading but it gets more confusing to me. would
you please could give me an example on how to prepare the system call
with those strings
 
I

Ian Collins

Atropo said:
Again sorry, but I don't understand what you mean.

Google does.
about my issue, you told me on the other similar post to use
ostringstream. I'm reading but it gets more confusing to me. would
you please could give me an example on how to prepare the system call
with those strings

std::eek:stringstream out;

out << whatever;

system( out.str().c_str() );
This is a signature.
 
A

Atropo

Again sorry, but I don't understand what you mean.

Google does.
about my issue, you told me on the other similar post to use
ostringstream. I'm reading but it gets more confusing to me.   would
you please could give me an example on how to prepare the system call
with those strings

std::eek:stringstream out;

out << whatever;

system( out.str().c_str() );

This is a signature.

Thanks Ian. as I told you I'm on the very beginnings of c++. I
included your code but compiler throws error about not function
osstringstream defined. what library do i need to include??

meanwhile kept the string var, now it doesn't complaint about
asignation and the cout shows well what i want, but not the system
call. do i need to include something on the system call

strt ="date -u "+ strMM+ strDD+ strhh+ strmm+ strAA+ "." + strss +
" 2>&1 >/dev/null;";
cout << strt;

system(strt);


rtchk2.cpp:62: cannot convert `string' to `const char *' for argument
`1' to `system (const char *)'

maybe something like the example you gave me.
system( out.str().c_str() )
in this case you defined out, but to use it you put out.str().c_str()
is there something similar for the strings ??
 
I

Ian Collins

*Please* stop quoting signatures!
Thanks Ian. as I told you I'm on the very beginnings of c++. I
included your code but compiler throws error about not function
osstringstream defined. what library do i need to include??
You don't include a library, you include a header. osstringstream ind
declared in <sstream>.

Your C++ book should have told you this, which one are you using?
meanwhile kept the string var, now it doesn't complaint about
asignation and the cout shows well what i want, but not the system
call. do i need to include something on the system call

strt ="date -u "+ strMM+ strDD+ strhh+ strmm+ strAA+ "." + strss +
" 2>&1 >/dev/null;";
cout << strt;

system(strt);
Well that's not surprising, is it? You changed the operator.
rtchk2.cpp:62: cannot convert `string' to `const char *' for argument
`1' to `system (const char *)'

Very true. Look up std::string's c_str() method. Look up what
std::eek:sstringstream's str() method returns.

You should do some background reading on strings and iostreams.
 
S

Stephen Horne

....
system(strt);

Ian already provided the answer, really. The .str () method for
ostringstream gives a string, but he needed to use .c_str () as well
before calling system.

Even though you're accessing your string directly, you also need the
..c_str ()

system(strt.c_str ());


It's a bit of C heritage. There are two styles of strings - char
arrays and the string class. The system call needs a char array (or
rather a pointer to one).

The string class is definitely the thing to use when you can, but
sometimes you can't avoid the char arrays.
 
A

Atropo

*Please* stop quoting signatures!


You don't include a library, you include a header.  osstringstream ind
declared in <sstream>.

Your C++ book should have told you this, which one are you using?
Actually dont have any. which one would you recommend me?? for free
download of course, maybe you realize by my bad english that english
is not my primary languaje
Well that's not surprising, is it?  You changed the operator.
Well I just use somes examples to twist a bit.
Very true.  Look up std::string's c_str() method.  Look up what
std::eek:sstringstream's str() method returns.
very useful tip, I'm RTFM
I'll give a try to system( strt.c_str() ); I'm just guessing here.
You should do some background reading on strings and iostreams.
I know , but when you know nothing, usually don't know what to look
for.
 
A

Atropo

Ian already provided the answer, really. The .str () method for
ostringstream gives a string, but he needed to use .c_str () as well
before calling system.

Even though you're accessing your string directly, you also need the
.c_str ()

system(strt.c_str ());

It's a bit of C heritage. There are two styles of strings - char
arrays and the string class. The system call needs a char array (or
rather a pointer to one).

The string class is definitely the thing to use when you can, but
sometimes you can't avoid the char arrays.

Thanks a lot Stephen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top