[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 paste strings to make a command for system

Atropo

10/15/2008 10:12:00 PM

Hi all.

Having several strings how do i combine them to construct a command;
lets say to run the date command.

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

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

system(strtarget);

Not sure if the problem it's syntax in the strtarget line (59). when
I compile i get this

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

Thanks in advance. really appreciate any advise.

13 Answers

Ian Collins

10/15/2008 10:22:00 PM

0

Atropo wrote:
> Hi all.
>
> Having several strings how do i combine them to construct a command;
> lets say to run the date command.
>
> string str = "14/10/08 19:06:09";
> strDD = str.substr(0,2);
> strMM = str.substr(3,2);
> strAA = str.substr(6,2);
> strhh = str.substr(9,2);
> strmm = str.substr(12,2);
> strss = str.substr(15,2);
>
> strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<strss<<
> " 2>&1 /dev/null;"<<endl;
>
> system(strtarget);
>
> Not sure if the problem it's syntax in the strtarget line (59). when
> I compile i get this
>
> rtchk.cpp: In function `int main (int, char **)':
> rtchk.cpp:59: no match for `string & << const char[9]'
>
> Thanks in advance. really appreciate any advise.
>
std::string (I assume you are using std::string?) does not have an
operator <<.

You should be using an ostringstream.

--
Ian Collins

Atropo

10/15/2008 11:50:00 PM

0

On 15 oct, 17:22, Ian Collins <ian-n...@hotmail.com> wrote:
> Atropo wrote:
> > Hi all.
>
> > Having several strings how do i combine them to construct a command;
> > lets say to run the date command.
>
> >        string str   = "14/10/08 19:06:09";
> >    strDD = str.substr(0,2);
> >    strMM = str.substr(3,2);
> >    strAA = str.substr(6,2);
> >    strhh = str.substr(9,2);
> >    strmm = str.substr(12,2);
> >    strss = str.substr(15,2);
>
> > strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<strss<<
> > " 2>&1 /dev/null;"<<endl;
>
> >         system(strtarget);
>
> > Not sure if the problem it's syntax in the strtarget line (59).  when
> > I compile i get this
>
> > rtchk.cpp: In function `int main (int, char **)':
> > rtchk.cpp:59: no match for `string & << const char[9]'
>
> > Thanks in advance.  really appreciate any advise.
>
> std::string (I assume you are using std::string?)  does not have an
> operator <<.
>
> You should be using an ostringstream.
>
> --
> Ian Collins- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

sorry Ian but I'm a really newbie. how should I use the
ostringstream ??

Ian Collins

10/16/2008 12:27:00 AM

0

Atropo wrote:
>
> sorry Ian but I'm a really newbie. how should I use the
> ostringstream ??

The same way as you use any other ostream, such as std::cout. In your
case, replace the string you were trying to stream to with an ostringstream.

Look up how to use the str() method to get the buffer's string.

--
Ian Collins

Stephen Horne

10/16/2008 3:15:00 AM

0

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

>sorry Ian but I'm a really newbie. how should I use the
>ostringstream ??

This is from memory so could be wrong, but should be close...


std::ostringstream x;

x << "here is some text" << 847 << "whatever";

std::cout << x.str () << std::endl;

The point is, std::ostringstream acts like any other stream in terms
of how you stream data into it, but it buffers that data rather than
outputing it. The str method (I think) provides access to the complete
buffered string.

Juha Nieminen

10/16/2008 1:41:00 PM

0

Ian Collins wrote:
> std::string (I assume you are using std::string?) does not have an
> operator <<.
>
> You should be using an ostringstream.

Given that the string is constructed exclusively with other strings,
you could simply append them with the + operator, ie:

strtarget = "date -u " + strMM + strDD + ...

OTOH, IMO the whole approach is just wrong. The rule of thumb is that
if you feel the need to use the 'system()' function you are doing things
in the wrong way.

Given that he is apparently trying to get a date in a specific format,
there actually exists a *standard* function which does more or less the
same thing as the unix 'date' command: strftime().

Not only will it be more portable to use that, it will also be a lot
more efficient.

Juha Nieminen

10/16/2008 1:43:00 PM

0

Atropo wrote:
> strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<strss<<
> " 2>&1 /dev/null;"<<endl;
>
> system(strtarget);

You really want to look at the strftime() standard function rather
than doing that ugly hack with the system() function. strftime() is not
only more portable, it's also a lot more efficient.

James Kanze

10/17/2008 7:24:00 AM

0

On Oct 16, 3:41 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Ian Collins wrote:
> > std::string (I assume you are using std::string?) does not
> > have an operator <<.

> > You should be using an ostringstream.

> Given that the string is constructed exclusively with other
> strings, you could simply append them with the + operator, ie:

> strtarget = "date -u " + strMM + strDD + ...

Yes, but it's less flexible (because it requires everything to
be a string), and less idiomatic.

> OTOH, IMO the whole approach is just wrong. The rule of thumb
> is that if you feel the need to use the 'system()' function
> you are doing things in the wrong way.

Why? If portability isn't a concern (or in some cases, even if
it is---I use common shell scripts and make files under Windows
and Unix), and there is a system function which does exactly
what you need done, why not use it, rather than reimplement it
in your own code?

> Given that he is apparently trying to get a date in a specific
> format, there actually exists a *standard* function which does
> more or less the same thing as the unix 'date' command:
> strftime().

That's true. The problem is that he already has a date in
string format. All he is really trying to do is reorder fields
and change the separator, and the fact that it is a date is more
or less irrelevant. On the other hand, parsing his input into
a tm structure, and then using strftime, would allow more
flexibility with regards to the input format, e.g. 20/6/2008 for
the date, for example.

> Not only will it be more portable to use that, it will also be
> a lot more efficient.

In his case, using strftime would be less efficient, because it
involves a conversion to int and back. But frankly, who cares.
I'm pretty sure that all of the suggestions so far are efficient
enough; it's not the sort of thing you're likely to find in a
tight loop or a critical path.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

10/17/2008 7:34:00 AM

0

On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Atropo wrote:
> > strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<strss<<
> > " 2>&1 /dev/null;"<<endl;

> > system(strtarget);

> You really want to look at the strftime() standard function
> rather than doing that ugly hack with the system() function.

The system function is being used to set the date. Neither
strftime() nor any other standard function will do that. (In
this case, using system is more portable than any of the
alternatives; it will work on any Unix system, and on Windows if
a Unix toolkit is accessible in your path.)

> strftime() is not only more portable, it's also a lot more
> efficient.

strftime() doesn't do what he wants, and any use of it here
would doubtlessly be less efficient, since it involves
conversions to int and back. (And what's all this business
about efficiency, anyway?)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Juha Nieminen

10/17/2008 11:55:00 AM

0

James Kanze wrote:
>> Not only will it be more portable to use that, it will also be
>> a lot more efficient.
>
> In his case, using strftime would be less efficient, because it
> involves a conversion to int and back.

Are you seriously claiming that getting a proper struct tm instance
and calling strftime is less efficient than calling system(), which
usually spawns a command-line interpreter, parses the command and its
parameters, starts a new process with the program specified in the
command line, runs the program and returns the result? Especially since
the program in question ('date') probably itself also constructs a
struct tm instance and calls strftime.

Juha Nieminen

10/17/2008 11:58:00 AM

0

James Kanze wrote:
> On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>> Atropo wrote:
>>> strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<strss<<
>>> " 2>&1 /dev/null;"<<endl;
>
>>> system(strtarget);
>
>> You really want to look at the strftime() standard function
>> rather than doing that ugly hack with the system() function.
>
> The system function is being used to set the date.

At least here the -u parameter specifies that the date should be in
UTC. It says nothing about setting the date.

> strftime() doesn't do what he wants, and any use of it here
> would doubtlessly be less efficient, since it involves
> conversions to int and back.

How can a call to strftime() be less efficient than spawning a
command-line interpreter and running the 'date' command, which itself
most probably calls strftime()?