473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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<<"."<<str ss<<
" 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.

Oct 15 '08 #1
12 1981
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<<"."<<str ss<<
" 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
Oct 15 '08 #2
On 15 oct, 17:22, Ian Collins <ian-n...@hotmail.comwrote:
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<<"."<<str ss<<
" 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 ??
Oct 15 '08 #3
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
Oct 16 '08 #4
On Wed, 15 Oct 2008 16:50:02 -0700 (PDT), Atropo <lx*******@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.

Oct 16 '08 #5
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.
Oct 16 '08 #6
Atropo wrote:
strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<str ss<<
" 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.
Oct 16 '08 #7
On Oct 16, 3:41 pm, Juha Nieminen <nos...@thanks.invalidwrote:
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:ja*********@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
Oct 17 '08 #8
On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Atropo wrote:
strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<str ss<<
" 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:ja*********@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

Oct 17 '08 #9
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.
Oct 17 '08 #10
James Kanze wrote:
On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks.invalidwrote:
>Atropo wrote:
>>strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<str ss<<
" 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()?
Oct 17 '08 #11
On Oct 20, 2:20*pm, ytrem...@nyx.nyx.net (Yannick Tremblay) wrote:
In article <E5%Jk.67$1K...@read4.inet.fi>,
Juha Nieminen *<nos...@thanks.invalidwrote:
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.

--
James Kanze (GABI Software) email:ja*********@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
Oct 20 '08 #12
On Oct 17, 1:58*pm, Juha Nieminen <nos...@thanks.invalidwrote:
James Kanze wrote:
On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Atropo wrote:
strtarget<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strAA<<"."<<str ss<<
" 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.
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.
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()?
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.

--
James Kanze (GABI Software) email:ja*********@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

Oct 20 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Trevor | last post by:
I have a couple of questions regarding C# strings. 1) Sometimes I see a string in someone else's code like: string foo = @"bar"; What does the '@' do for you? 2) Is there a performance...
5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
2
by: Keith | last post by:
I'm trying to come up with a way to create a contextmenu that will do all the "standard" functions (cut, copy, paste, undo, etc). There seems to be a lot of information out there - but nothing...
2
by: 78 | last post by:
is there any way I can make my vb.net app paste data to a program or desktop field? 78
6
by: Christian Blackburn | last post by:
Hi Gang, When encoding HTML strings it'll convert things like " --> &rsquo and the like using Server.HTMLEncode(). However, is there a command to make sure strings don't contain valid SQL...
8
by: serge calderara | last post by:
Dear all, I have an treeview control with different node object, I would like to implement the Copy/Paste function of an object . For that I am using the folowing function to copy teh object to...
17
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have...
4
by: BartlebyScrivener | last post by:
Using Python on Debian Etch. What is the best way to paste a block of text in at the command prompt. I'm trying something like: Quote = raw_input("Paste quote here: ") Which works great...
10
by: Atropo | last post by:
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 =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.