NEWBIE: how to paste strings to make a command for system()

A

Atropo

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

Ian Collins

Atropo said:
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.
 
A

Atropo

Atropo said:
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.

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

Ian Collins

Atropo said:
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.
 
S

Stephen Horne

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::eek:stringstream x;

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

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

The point is, std::eek:stringstream 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.
 
J

Juha Nieminen

Ian said:
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.
 
J

Juha Nieminen

Atropo said:
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.
 
J

James Kanze

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

James Kanze

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?)
 
J

Juha Nieminen

James said:
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.
 
J

Juha Nieminen

James said:
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()?
 
J

James Kanze

Efficiency is unlikely to be important.  strftime would be of
no use because the date is in human readable format as
suggested above, not as a tm struct.  He could possibly use
strptime to get a tm then strftime to get a string back but
simple string manipulation would do the job at least as well
and probably faster.

Just a nit, but there is no strptime in standard C++. It's a
Unix extension.
You are not comparing strftime with system(), you are
comparing strftime+strptime with string manipulation. (but
efficiency is very unlikely to be critical)

Compared to the time necessary to spawn the child process, very,
very unlikely. Depending on what his real specification is,
there can be an advantage in passing through the numeric values
and struct tm. It's more flexible, even if it is more work.
But you still have to parse the input (boost::regex seems the
best solution for that), then if you want numeric values, use
istringstream to convert the parsed substrings. (If strptime is
available, it might be slightly easier to use, but IMHO,
learning to use boost::regex for this sort of thing is worth the
effort.) Then you reformat the data you have to get what you
want; if it's all strings, you can use either ostringstream or
string concatenation; if you have numeric values, it has to be
ostringstream.
 
J

James Kanze

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

The command he generated is the command to set the system time
under Unix. And using system is probably the most portable way
of doing it; it will work under all Unix, where as all of the
other ways will only work under a specific Unix.
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()?

Because he still needs to spawn a command line and execute an
external function to set the system time. All using strftime()
means is that he absolutely has to convert the numeric values in
his string to numbers (ints), and that he also has to program
around buffer length issues.
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top