473,385 Members | 2,162 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.

system command

Hello,

I am writing a script which is using the system() command. In the
script the system command invokes an encryption with system("gpg -f
file.txt").

The problem comes when the script is required to perform this
operation on different files which the user selects via another
function. The name of the filename is stored in a variable as a
string and I can't get the variable substituted into the ststem
command. I have tried to concatenate the whole command inside the
quotes into one string variable and enter it into the system command
but this brings up the error that the system command requires a const
char and not strings. Can anyone help me out with getting this system
command to work.

I have had the thought of looking into tcreating a pointer to the
string and putting this in the command, would this be on the right
track?

any help you can give would be greatly appreciated.

Thanks and regards

John
Jul 23 '05 #1
15 7878
Sokar wrote:
I am writing a script which is using the system() command. In the
script the system command invokes an encryption with system("gpg -f
file.txt").

The problem comes when the script is required to perform this
operation on different files which the user selects via another
function. The name of the filename is stored in a variable as a
string and I can't get the variable substituted into the ststem
command. I have tried to concatenate the whole command inside the
quotes into one string variable and enter it into the system command
but this brings up the error that the system command requires a const
char and not strings. Can anyone help me out with getting this system
command to work.

I have had the thought of looking into tcreating a pointer to the
string and putting this in the command, would this be on the right
track?


...
std::string onepart("gpg -f ");
std::string twopart;
// obtain twopart somehow ...
twopart = "file.txt";
system((onepart+twopart).c_str());

V
Jul 23 '05 #2
>>this brings up the error that the system command requires a const
char and not strings.
Use std::string::c_str();
I have had the thought of looking into tcreating a pointer to the
string and putting this in the command, would this be on the right
track?


No, because then you would be passing it a std::string * and not a
const char *

Jul 23 '05 #3
The STL 'string' class has a '.c_str()' method that "Returns a pointer
to a null-terminated array of characters representing the string's
contents."
#include <string>
#include <cstdlib>
int
main()
{
std::string quine("perl -e 'while(<>) { print; }' quine.cpp");
||||
++++---+++
// not a real quine, |||
// since it doesn't print all
// of it's source code.

std::system( quine.c_str() );
}

Jul 23 '05 #4
On 5 Apr 2005 06:02:37 -0700, Sokar <jm**********@hotmail.com> wrote:
Hello,

I am writing a script which is using the system() command. In the
script the system command invokes an encryption with system("gpg -f
file.txt").

The problem comes when the script is required to perform this
operation on different files which the user selects via another
function. The name of the filename is stored in a variable as a
string and I can't get the variable substituted into the ststem
command. I have tried to concatenate the whole command inside the
quotes into one string variable and enter it into the system command
but this brings up the error that the system command requires a const
char and not strings. Can anyone help me out with getting this system
command to work.

I have had the thought of looking into tcreating a pointer to the
string and putting this in the command, would this be on the right
track?


not exactly.

string::c_str() gives you a pointer to a zero-terminated char - sequence
usually you can write:

void fun(char* c);
....
string s = "ssss";
fun(s.c_str())

i hope i understood your problem correctly.

- ulrich
Jul 23 '05 #5
> system((onepart+twopart).c_str());
Is this safe? I guess the temporary string will be destructed and the
pointer returned by c_str will no longer be vaild.

Andre Caldas.
Jul 23 '05 #6
Andre wrote:
system((onepart+twopart).c_str());

Is this safe? I guess the temporary string will be destructed and the
pointer returned by c_str will no longer be vaild.


Yep.
But the temporary will be destroyed at the end of the statement.
That is, when the system() call has already returned. Thus
it is safe.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7

Andre Caldas wrote:
Karl Heinz Buchegger wrote:
system((onepart+twopart).c_str());

Is this safe? I guess the temporary string will be destructed and thepointer returned by c_str will no longer be vaild.
But the temporary will be destroyed at the end of the statement.
That is, when the system() call has already returned. Thus
it is safe.


I think it will be destructed after c_str() returns. This is before

the system call is returned.


That would make it pretty useless. What could you do with it? I mean,
you'd have a member function that returns a pointer that is always
invalid.

Brian

Jul 23 '05 #8
> The temporary (onepart+twopart) is passed to c_str()...

The temporary is created in system's scope, and then a pointer or
constant reference to the temporary is passed to string::c_str().

Jul 23 '05 #9
> #include <iostream>

class CTest
{
public:
CTest() { std::cout << "Ctor called" << std::endl; }
~CTest() { std::cout << "Dtor called" << std::endl; }

const char* c_str() { return "Test"; }
};

void foo( const char* Arg )
{
std::cout << Arg << std::endl;
}

int main()
{
foo( CTest().c_str() );
}

Output:
Ctor called
Test
Dtor called

The temporary is destroyed after the call has returned.


That is really new to me! I am really surprised.
Thanks for the example :-)
Jul 23 '05 #10
> The temporary is created in system's scope, and then a pointer or
constant reference to the temporary is passed to string::c_str().


That makes a lot of sense. ("scope" is much better then "end of the
statement")

Thank you.
Andre Caldas.
Jul 23 '05 #11
Andre Caldas wrote:
Karl Heinz Buchegger wrote:
system((onepart+twopart).c_str());

Is this safe? I guess the temporary string will be destructed
and the pointer returned by c_str will no longer be vaild.


But the temporary will be destroyed at the end of the statement.
That is, when the system() call has already returned. Thus
it is safe.


I think it will be destructed after c_str() returns. This is
before the system call is returned.


Temporaries live until the end of the full-expression in which
they were created (at least). "full-expression" roughly means a
statement, or the controlling expression in a loop.

Jul 23 '05 #12
> Temporaries live until the end of the full-expression in which
they were created (at least). "full-expression" roughly means a
statement, or the controlling expression in a loop.


Sorry, but "full-expression" means nothing to me. Is it on some standard?

I guess my "rough" understanding of "full-expression" was different from
everybody else's. Now I understand how it works. It is much better then
the way I thought it was.

But by the way, I don't belive that the destruction of the temporary
would make things like "a(temporary().d())" - as long as temporary::d()
does not return a reference to any thing that would be destructed as well.

Andre Caldas.
Jul 23 '05 #13
Correction:
would make things like "a(temporary().d())" - as long as temporary::d()


would make things like "a(temporary().d())" useless - as long as
temporary::d()
Jul 23 '05 #14
Andre Caldas wrote:
Temporaries live until the end of the full-expression in which
they were created (at least). "full-expression" roughly means a
statement, or the controlling expression in a loop.
Sorry, but "full-expression" means nothing to me.


That's why I explained it..
Is it on some standard?
Yes -- the C and C++ standards
I guess my "rough" understanding of "full-expression" was
different from everybody else's. Now I understand how it
works. It is much better then the way I thought it was.
That's good :)
But by the way, I don't belive that the destruction of
the temporary would make things like "a(temporary().d())"
useless - as long as temporary::d() does not return a
reference to any thing that would be destructed as well.


That expression is perfectly fine. However I'm not sure if
this is a great example; you could still call a() just fine
even if the temporary were destroyed immediately after the
call to d(). A better example might be:

struct foo {
std::string s;
std::string &d() { return s; }
};
printf("%s\n", foo().d().c_str());

which is fine because the temporary foo is not destroyed
until after the printf statement has finished executing.

Jul 23 '05 #15
Old Wolf wrote:
Temporaries live until the end of the full-expression in which
they were created (at least). "full-expression" roughly means a
statement, or the controlling expression in a loop.


Sorry, but "full-expression" means nothing to me.


That's why I explained it..


I just wanted to say that I don't know the formal defition of
"full-expression", and I don't think that if it is not "well-defined"
you cannot use it to argue. (but since you tell me that
'full-expression' is defined on the standards... then it's OK)

But by the way, I don't belive that the destruction of
the temporary would make things like "a(temporary().d())"
useless - as long as temporary::d() does not return a
reference to any thing that would be destructed as well.

That expression is perfectly fine. However I'm not sure if
this is a great example; you could still call a() just fine
even if the temporary were destroyed immediately after the
call to d(). A better example might be:

struct foo {
std::string s;
std::string &d() { return s; }
};
printf("%s\n", foo().d().c_str());

which is fine because the temporary foo is not destroyed
until after the printf statement has finished executing.


This example does not conform to the hipotetical situation where *foo*
would infact be destroyed.

Andre Caldas.
Jul 23 '05 #16

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

Similar topics

8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
6
by: JLK | last post by:
I'm having a bit of a time with the following code. I can script this real easy in Bash but I'm trying to practice my C++: ******************************************************* #include...
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
1
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by...
3
by: Patrick.O.Ige | last post by:
I'm loading an Array below but getting the error "Object reference not set to an instance saying 'ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text' is the error line. I DON'T...
1
by: Brad | last post by:
I have an issue trying to execute commands in a web service. The command starts under the specified user. However, it never completes its execution. I can execute simple commands like "echo HELLO"...
11
by: jobs239 | last post by:
Can I use this line inside C program "system(java -jar <jarfilename>)" to run a java program from C? Or do I have to use some JNI interface.?
7
by: Mark Rae | last post by:
Hi, Has anyone successfully used the FTP stuff in the System.Net namespace against a VMS FTP server? I'm trying to do this at the moment and can't even get a directory listing, although there...
23
by: student.matt | last post by:
ok i am trying to solve this little problem i have my program waits for the 1st system() call to finish before starting the next i will just throw an example of what i mean out in this example...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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.