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

Capture output from system command

109 100+
I am running a system command and I need to capture the output so that, if the command has done what it is meant to, the program does something different. If I do this:

Expand|Select|Wrap|Line Numbers
  1. int i;
  2. i=system("cd C:\\Dave\\C-programming\\Password Cracker\\ & ftp -s:ftpscript.txt ftp.collateraldesign.co.uk");
  3. printf ("The value returned was: %d.\n",i);
The value returned is 0. However, I want to know if there was an error logging on, how can I know whether the command was successful or not?
Jun 30 '07 #1
6 16876
weaknessforcats
9,208 Expert Mod 8TB
The system() command passes your argument to the correct command interpreter and returns whatever the command inrterpreter returns.

You should be able to capture the return value from system().

Check your docs.
Jun 30 '07 #2
niskin
109 100+
Ok, the only problem is it always returns 0 if the command is carried out successfully, but I would prefer it to say something like "connection successful/unsuccessful", at least something to distinguish between success and failure.
Jun 30 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Isn't the 0 enough for success and !0 is failure??

Otherwise, you will get into a soggy area of return codes, errnos and stuff like that which may not be consistent from one command interpreter to the next.

More than this an you may have to write the code to process the request you send to the system() command yourself.
Jun 30 '07 #4
niskin
109 100+
Ok, is there an alternative to the system command? I have heard of popen and exec but I am not clear on them.
Jun 30 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
popen creates a pipe. You run your command as a spawned thread and the output of the thread goes into one end of the pipe in memory leaving you with the ability to read the output from the pipe as though it were in a disc file.

You might check this one out.

I'm not sure what exec is.

Here is a link to the Microsoft docs for popen.
Jul 1 '07 #6
niskin
109 100+
Cheers, that's really helpful.
Jul 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Frank Teoh | last post by:
I am trying to capture the output of os.system() into a string without success. Since os.system() returns only its exit_status, how can I do that? system(...) system(command) -> exit_status ...
8
by: standish22 | last post by:
I want to programmatically capture an error that is done from a Shell command in VB.Net code. What I have is a Test.sql file that looks like ---------------------------------------------------...
1
by: Ernesto | last post by:
I'm looking for a way to capture command line output from a cmd session. Is there a way to use python to launch the application from the beggining.... then stream all of the output to a text...
0
by: j101 | last post by:
I am attempting to set up Q Capture on RH Linux (x86_64) using DB2 9 fp2, but there seems to be a general problem loading a specify MQ shared library "/opt/mqm/lib/libmqm_r.so". I have MQ v6...
5
by: Muffin | last post by:
I am trying to capture the out put of a command line program. Let say ping or maybe better yet nslookup. I would like to launch and then capture all the output , redirect it I guess to a string...
10
by: lalnamar | last post by:
I want to execute a unix time command using perl, and capture the output (specifically, the time it takes the command to execute). i tried using the system command: system("time command ");...
2
by: jdbartlett | last post by:
I'm trying to capture output from a command line utility (XMLSec), but only get an empty result. If I call the script from the command line, I see XMLSec's output, but I can't seem to capture it!...
1
by: chitralaxman | last post by:
sir, i've written a program which will pass input to an exe file using system command and now i'm in need to capture the output produced by the .exe file for that input . the output should be...
1
Oryx
by: Oryx | last post by:
In another thread, someone posted... Well, I tried this, even using a different variable for the STDOUT lines: $output = exec($command . ' 2>&1', $outarr, $return); No joy. Even though...
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...
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...
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
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,...
0
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...

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.