[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

NEWBIE: how to construct a system call from strings

Atropo

10/15/2008 10:52:00 PM

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.
10 Answers

Ian Collins

10/15/2008 10:54:00 PM

0

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

--
Ian Collins

Atropo

10/15/2008 11:47:00 PM

0

On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.com> wrote:
> 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..

Ian Collins

10/16/2008 12:25:00 AM

0

Atropo wrote:
> On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.com> wrote:
>> 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

Atropo

10/16/2008 12:46:00 AM

0

On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.com> wrote:
> Atropo wrote:
> > On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.com> wrote:
> >> 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

Ian Collins

10/16/2008 12:53:00 AM

0

Atropo wrote:
> On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.com> wrote:
>> Atropo wrote:
>>> On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.com> wrote:
>>>> 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

Atropo

10/16/2008 2:17:00 AM

0

On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.com> wrote:
> Atropo wrote:
> > On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.com> wrote:
> >> Atropo wrote:
> >>> On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.com> wrote:
> >>>> 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 ??

Ian Collins

10/16/2008 2:29:00 AM

0

Atropo wrote:
> On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.com> wrote:

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

Stephen Horne

10/16/2008 3:24:00 AM

0

On Wed, 15 Oct 2008 19:16:51 -0700 (PDT), Atropo <lxvasquez@gmail.com>
wrote:

>On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.com> wrote:

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

Atropo

10/16/2008 4:42:00 AM

0

On 15 oct, 21:28, Ian Collins <ian-n...@hotmail.com> wrote:
> Atropo wrote:
> > On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.com> wrote:
> >> 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

Atropo

10/16/2008 4:56:00 AM

0

On 15 oct, 22:24, Stephen Horne <sh006d3...@blueyonder.co.uk> wrote:
> 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.com> wrote:
> >> 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.