James Kanze
10/17/2008 7:24:00 AM
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