473,806 Members | 2,717 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
12 2015
James Kanze wrote:
On Oct 16, 3:42 pm, Juha Nieminen <nos...@thanks. invalidwrote:
>Atropo wrote:
>>strtarget<<"d ate -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.
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.ny x.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+strpti me 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 objektorientier ter Datenverarbeitu ng
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<<"da te -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.
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 objektorientier ter Datenverarbeitu ng
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
2399
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
21042
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
2690
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
1488
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
5748
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
2619
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
5140
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
1822
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
9599
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,...
0
10624
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10111
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...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
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...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
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.