473,799 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
" 2>&1 /dev/null;"<<endl;

system(strtarge t);

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 2014
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<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
" 2>&1 /dev/null;"<<endl;

system(strtarge t);

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.co mwrote:
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<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
" 2>&1 /dev/null;"<<endl;
* * * * system(strtarge t);
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*******@gmai l.com>
wrote:
>sorry Ian but I'm a really newbie. how should I use the
ostringstrea m ??
This is from memory so could be wrong, but should be close...
std::ostringstr eam x;

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

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

The point is, std::ostringstr eam 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<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
" 2>&1 /dev/null;"<<endl;

system(strtarge t);
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 objektorientier ter Datenverarbeitu ng
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<<"dat e -u "<<strMM<<strDD <<strhh<<strmm< <strAA<<"."<<st rss<<
" 2>&1 /dev/null;"<<endl;
system(strtarge t);
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 objektorientier ter Datenverarbeitu ng
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

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

Similar topics

20
2398
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 loss if I start concatenating strings together
5
21038
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 windows explorer: when you do a right-click on a file and choose Copy, and than paste it somewhere in my application and vice versa.
2
2686
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 seems to work for me. A few people refer to txt.copy() txt.paste(), etc. I'm not sure if those are old functions, but they do not work for me. I've tried textbox1.selectall() - that works in selecting
2
1487
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
5746
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 commands? Like I wouldn't want a string to contain "; Drop TableXYX;" or something along those lines. Thanks, Christian Blackburn
8
2616
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 clipboard : ====> Private Sub CopyToClipboard(ByVal Obj As PluginApp.PlugInReport) ' Creates a new data format.
17
5134
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 about 20 textboxes on the form). Also to ensure that the button can't get used if the cursor isn't in a textbox field. And to ensure the contents of the clipboard are "text" contents that have been cut/copied from one of the textboxes on the form. ...
4
5451
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 for one line of text with a single newline. It gets
10
1819
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 = str.substr(3,2); strYY = str.substr(6,2);
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.