C++ using system()


 
Thread Tools Search this Thread
Top Forums Programming C++ using system()
# 1  
Old 10-12-2006
Question C++ using system()

I'm attempting to execute a system command contained within a variable, such as;

abfile = "fred"
abdest = "/home"
ab = "scp " + abfile + abdest;
system(ab);

abfile & abdest will be populated via a prompt so will differ each time the script is run.

The system command does not do anything with the variable, but works fine if I put the command in place of the 'ab' variable, should I be using something other than system to execute the contents of the variable ?
# 2  
Old 10-12-2006
system() takes a C string. You're giving it a C++ string, it'll have to be converted before system(), a C function, can use it. Try system(ap.c_str());
# 3  
Old 10-12-2006
MySQL

That works, many thanks.
# 4  
Old 11-03-2006
Quote:
abfile = "fred"
abdest = "/home"
ab = "scp " + abfile + abdest;
system(ab);
i think, that you should use sprintf.

like this:
Code:
abdest = "/home";
sprintf(ab,"%s %s%s", scp, abfile, abdest);
system(ab);

if you do not understand this, check sprintf manual on cplusplus.com Smilie
# 5  
Old 11-03-2006
I don't think that will work, since none of those are C-strings.
# 6  
Old 11-03-2006
Quote:
Originally Posted by Corona688
I don't think that will work, since none of those are C-strings.
eu, i am sorry, i didnt realize that we're talking about C (not c++) Smilie

edit: but in the title, there is " Question C++ using system() ", we are using c++, so we can save that values to *char, it should work then..
# 7  
Old 11-03-2006
Quote:
Originally Posted by zero0x
eu, i am sorry, i didnt realize that we're talking about C (not c++) Smilie
sprintf is a C function, it cannot use a C++ string object as it's output, even in C++. C++ strings cast to const char *, not char *, and that's an important distinction -- it's const because you're not supposed to write to it. There's no way to overload accesses to a pointer -- if you do, it's no longer a pointer -- so it'll never know to allocate memory to hold what you're writing to it. For example, if it's holding "Hello", and you write "hello12345676" to it, the "12345676" bit might overwrite class members or anything else nearby in the heap.

They will work as inputs, as long as you use object.c_str() instead of just object.

Last edited by Corona688; 11-03-2006 at 10:54 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Migrating jobs from COBOL Mainframe system to UNIX system

In a nutshell requirement is to migrate the system from mainframe environment to UNIX environment (MF cobol would be used I guess). I have not much of idea in this field. I need to do some investigation on following points - - Ease of conversion - Known Data compatibility issue - Issue in... (9 Replies)
Discussion started by: Tjsureboy4me
9 Replies

2. AIX

Cloning a system via mksysb backup from one system and restore to new system

Hello All, I am trying to clone an entire AIX virtual machine to a new virtual machine including all partitions and OS.Can anyone help me on the procedure to follow? I am not really sure on how it can be done.Thanks in advance. Please use CODE tags for sample input, sample output, and for code... (4 Replies)
Discussion started by: gull05
4 Replies

3. AIX

Accessing files on AIX system from Linux system

I have a following requirement in production system 1 : LINUX User: abcd system 2: AIX (it is hosting a production DB) Requirement user abcd from system 1 should have read access on archive log files created by DB on system 2. The log files are created with permissions 540 by user ora ,... (2 Replies)
Discussion started by: amitnm1106
2 Replies

4. Solaris

System hangs (freezes) on system bell/beep

I am running OpenIndiana development version oi_148 32-bit on a seven-year-old Dell Inspiron 8600. Seems to be running fine except for one particular annoyance: It freezes whenever a system bell/beep plays. I have mitigated this by turning the system bell off in gnome-terminal, which I use... (3 Replies)
Discussion started by: DeadBadger
3 Replies

5. UNIX for Advanced & Expert Users

how to make a full system backup excluding data and restoring it to a new system

Hi, In order to have a sand box machine that I could use to test some system changes before going to production state, I'd like to duplicate a working system to a virtual one. Ideally, I'd like to manage to do it this way : - Make a full system backup excluding the user file system (this... (7 Replies)
Discussion started by: pagaille
7 Replies

6. Shell Programming and Scripting

how to delete files at booting of system (system startup)

hi all I have a problem how to write a shell script which delete files/folder form directory whenever system boot and copy last updated folder/file in the specified directory.pse help me ASAP. i write a script which copy files in directory.I want when system boot up using script it check whether... (1 Reply)
Discussion started by: shubhig15
1 Replies

7. SCO

file system not getting mounted in read write mode after system power failure

After System power get failed File system is not getting mounted in read- write mode (1 Reply)
Discussion started by: gtkpmbpl
1 Replies

8. Solaris

rsh commands not getting executed from Solaris 10 System to AIX System

Hi Friends, I am trying to execute rsh commands from Solaris 10 system to AIX system. When I give; Solaris10# rsh <hostname> ls -l , it gives me an error rshd : 0826-826 The host name for your address is not known At the same time, Solaris10# rsh <hostname> ---- gives me remote shell of... (25 Replies)
Discussion started by: jumadhiya
25 Replies
Login or Register to Ask a Question