473,378 Members | 1,500 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,378 software developers and data experts.

NEWBIE: how to construct a system call from strings

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);
strhh = str.substr(9,2);
strmm = str.substr(12,2);
strss = str.substr(15,2);

strT<<"date -u "<<strMM<<strDD<<strhh<<strmm<<strYY<<"."<<str ss<< "
2>&1 >/dev/null;"<<endl;

system(strT);

when I compile it throws

rtchk.cpp: In function `int main (int, char **)':
rtchk.cpp:59: no match for `string & << const char[9]'

so i could be a syntax error, but I'm newbie.
if there is a typo, I can't see it.
Oct 15 '08 #1
10 1785
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?

--
Ian Collins
Oct 15 '08 #2
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,

What's wrong with the answer I gave last time you asked?

--
Ian Collins
Sorry Ian. I though those post were deleted after hours refreshing
the webpage this post never shows..
Oct 15 '08 #3
Atropo wrote:
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
>Atropo wrote:
>>Hi all,
What's wrong with the answer I gave last time you asked?
*Please* don't quote signatures.
>
Sorry Ian. I though those post were deleted after hours refreshing
the webpage this post never shows..
Usenet isn't IM, you have to give messages time to propagate. That
dreadful google interface appears to make things worse.

--
Ian Collins
Oct 16 '08 #4
On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?

*Please* don't quote signatures.
Again sorry, but I don't understand what you mean.
>
Sorry Ian. *I though those post were deleted * after hours refreshing
the webpage this post never shows..

Usenet isn't IM, you have to give messages time to propagate. *That
dreadful google interface appears to make things worse.
Yo're absolutely right. but I'm RTFM.

about my issue, you told me on the other similar post to use
ostringstream. I'm reading but it gets more confusing to me. would
you please could give me an example on how to prepare the system call
with those strings
--
Ian Collins
Oct 16 '08 #5
Atropo wrote:
On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.comwrote:
>Atropo wrote:
>>On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?
*Please* don't quote signatures.
Again sorry, but I don't understand what you mean.
Google does.
about my issue, you told me on the other similar post to use
ostringstream. I'm reading but it gets more confusing to me. would
you please could give me an example on how to prepare the system call
with those strings
std::ostringstream out;

out << whatever;

system( out.str().c_str() );
>--
Ian Collins
This is a signature.

--
Ian Collins
Oct 16 '08 #6
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 19:25, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 17:54, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
Hi all,
What's wrong with the answer I gave last time you asked?
*Please* don't quote signatures.
Again sorry, but I don't understand what you mean.

Google does.
about my issue, you told me on the other similar post to use
ostringstream. I'm reading but it gets more confusing to me. * would
you please could give me an example on how to prepare the system call
with those strings

std::ostringstream out;

out << whatever;

system( out.str().c_str() );
--
Ian Collins

This is a signature.

--
Ian Collins
Thanks Ian. as I told you I'm on the very beginnings of c++. I
included your code but compiler throws error about not function
osstringstream defined. what library do i need to include??

meanwhile kept the string var, now it doesn't complaint about
asignation and the cout shows well what i want, but not the system
call. do i need to include something on the system call

strt ="date -u "+ strMM+ strDD+ strhh+ strmm+ strAA+ "." + strss +
" 2>&1 >/dev/null;";
cout << strt;

system(strt);
rtchk2.cpp:62: cannot convert `string' to `const char *' for argument
`1' to `system (const char *)'

maybe something like the example you gave me.
system( out.str().c_str() )
in this case you defined out, but to use it you put out.str().c_str()
is there something similar for the strings ??

Oct 16 '08 #7
Atropo wrote:
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
>std::ostringstream out;

out << whatever;

system( out.str().c_str() );
*Please* stop quoting signatures!
>
Thanks Ian. as I told you I'm on the very beginnings of c++. I
included your code but compiler throws error about not function
osstringstream defined. what library do i need to include??
You don't include a library, you include a header. osstringstream ind
declared in <sstream>.

Your C++ book should have told you this, which one are you using?
meanwhile kept the string var, now it doesn't complaint about
asignation and the cout shows well what i want, but not the system
call. do i need to include something on the system call

strt ="date -u "+ strMM+ strDD+ strhh+ strmm+ strAA+ "." + strss +
" 2>&1 >/dev/null;";
cout << strt;

system(strt);
Well that's not surprising, is it? You changed the operator.
rtchk2.cpp:62: cannot convert `string' to `const char *' for argument
`1' to `system (const char *)'
Very true. Look up std::string's c_str() method. Look up what
std::osstringstream's str() method returns.

You should do some background reading on strings and iostreams.

--
Ian Collins
Oct 16 '08 #8
On Wed, 15 Oct 2008 19:16:51 -0700 (PDT), Atropo <lx*******@gmail.com>
wrote:
>On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
>system( out.str().c_str() );
....
> system(strt);
Ian already provided the answer, really. The .str () method for
ostringstream gives a string, but he needed to use .c_str () as well
before calling system.

Even though you're accessing your string directly, you also need the
..c_str ()

system(strt.c_str ());
It's a bit of C heritage. There are two styles of strings - char
arrays and the string class. The system call needs a char array (or
rather a pointer to one).

The string class is definitely the thing to use when you can, but
sometimes you can't avoid the char arrays.

Oct 16 '08 #9
On 15 oct, 21:28, Ian Collins <ian-n...@hotmail.comwrote:
Atropo wrote:
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
std::ostringstream out;
out << whatever;
system( out.str().c_str() );

*Please* stop quoting signatures!
Thanks Ian. *as I told you I'm on the very beginnings of c++. *I
included your code but compiler throws error about not function
osstringstream defined. * what library do i need to include??

You don't include a library, you include a header. *osstringstream ind
declared in <sstream>.

Your C++ book should have told you this, which one are you using?
Actually dont have any. which one would you recommend me?? for free
download of course, maybe you realize by my bad english that english
is not my primary languaje
>
meanwhile kept the string var, now it doesn't complaint about
asignation and the cout shows well what i want, but not the system
call. do i need to include something on the system call
* *strt ="date -u "+ strMM+ strDD+ strhh+ strmm+ strAA+ "." + strss +
* * * * * *" 2>&1 >/dev/null;";
* *cout << strt;
* *system(strt);

Well that's not surprising, is it? *You changed the operator.
Well I just use somes examples to twist a bit.
>
rtchk2.cpp:62: cannot convert `string' to `const char *' for argument
`1' to `system (const char *)'

Very true. *Look up std::string's c_str() method. *Look up what
std::osstringstream's str() method returns.
very useful tip, I'm RTFM
I'll give a try to system( strt.c_str() ); I'm just guessing here.
>
You should do some background reading on strings and iostreams.
I know , but when you know nothing, usually don't know what to look
for.
--
Ian Collins
Oct 16 '08 #10
On 15 oct, 22:24, Stephen Horne <sh006d3...@blueyonder.co.ukwrote:
On Wed, 15 Oct 2008 19:16:51 -0700 (PDT), Atropo <lxvasq...@gmail.com>
wrote:
On 15 oct, 19:52, Ian Collins <ian-n...@hotmail.comwrote:
system( out.str().c_str() );
...
* *system(strt);

Ian already provided the answer, really. The .str () method for
ostringstream gives a string, but he needed to use .c_str () as well
before calling system.

Even though you're accessing your string directly, you also need the
.c_str ()

system(strt.c_str ());

It's a bit of C heritage. There are two styles of strings - char
arrays and the string class. The system call needs a char array (or
rather a pointer to one).

The string class is definitely the thing to use when you can, but
sometimes you can't avoid the char arrays.
Thanks a lot Stephen.
Oct 16 '08 #11

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

Similar topics

2
by: phpfrizzle | last post by:
Hi there, this might sound strange, but i need to construct a name of a variable: i have these vars (and loads more): $menu_ho = massageMe("$string_1"); $menu_do = massageMe("$string_2");...
12
by: Laser Lu | last post by:
Hello, everybody, do you know how to use this Grouping Construct? (?> ) I've found its reference on MSDN, but still can not understand it totally. The following is its description: ...
8
by: Ioannis Vranos | last post by:
In .NET (and C++/CLI) there is an overloaded String == operator for handles. That is when we do comparison of two String handles the contents of the Strings are compared instead of their addresses....
2
by: Ninan | last post by:
I have different 256 character length records stored in a file. The first character is the record identifier. I want to instantiate different record objects. I cannot change the records stored in...
1
by: sheephead86 | last post by:
Hi, I'm pretty new to java, and I have a small problem involving drawing a rectangle on a java applet.Firstly this is not a plea for someone to help me with this peice of work, I just need pointing...
5
by: kamikaze04 | last post by:
Hello. I have a very newbie question about Streams. The situation is that i have a function (that i cannot modify it's definition/call): public void F1(istream & in){ while( ...) { ...
0
by: Madmartigan | last post by:
Hi I'm a newbie to C# and have been instructed to create a Hangman game in SharpDevelop. I don't want the answer to the full code, just some help along the way. I have included my code thus...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
12
by: Atropo | last post by:
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 =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.