By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

NEWBIE: how to construct a system call from strings

P: n/a
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<<"."<<str ss<< "
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.
Oct 15 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?

--
Ian Collins
Oct 15 '08 #2

P: n/a
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,

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

--
Ian Collins
Sorry Ian. I though those post were deleted after hours refreshing
the webpage this post never shows..
Oct 15 '08 #3

P: n/a
Atropo wrote:
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
>Atropo wrote:
>>Hi all,
What's wrong with the answer I gave last time you asked?
*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.

--
Ian Collins
Oct 16 '08 #4

P: n/a
On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?

*Please* don't quote signatures.
Again sorry, but I don't understand what you mean.
>
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.
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
--
Ian Collins
Oct 16 '08 #5

P: n/a
Atropo wrote:
On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.comwrote:
>Atropo wrote:
>>On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?
*Please* don't quote signatures.
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::ostringstream out;

out << whatever;

system( out.str().c_str() );
>--
Ian Collins
This is a signature.

--
Ian Collins
Oct 16 '08 #6

P: n/a
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?
*Please* don't quote signatures.
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::ostringstream out;

out << whatever;

system( out.str().c_str() );
--
Ian Collins

This is a signature.

--
Ian Collins
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 ??

Oct 16 '08 #7

P: n/a
Atropo wrote:
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
>std::ostringstream out;

out << whatever;

system( out.str().c_str() );
*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::osstringstream's str() method returns.

You should do some background reading on strings and iostreams.

--
Ian Collins
Oct 16 '08 #8

P: n/a
On Wed, 15 Oct 2008 19:16:51 -0700 (PDT), Atropo <lx*******@gmail.com>
wrote:
>On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
>system( out.str().c_str() );
....
> 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.

Oct 16 '08 #9

P: n/a
On 15 oct, 21:28, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
std::ostringstream out;
out << whatever;
system( out.str().c_str() );

*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?
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
>
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.
Well I just use somes examples to twist a bit.
>
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::osstringstream'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.
--
Ian Collins
Oct 16 '08 #10

P: n/a
On 15 oct, 22:24, Stephen Horne <sh006d3...@blueyonder.co.ukwrote:
On Wed, 15 Oct 2008 19:16:51 -0700 (PDT), Atropo <lxvasq...@gmail.com>
wrote:
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
system( out.str().c_str() );
...
* *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.
Thanks a lot Stephen.
Oct 16 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.