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

Get system() error message

Hi,

In the program below:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

using namespace std;
int main()
{
system("ls asdf");

printf("%d\n", errno);

return 0;
}
the output is:

ls: asdf not found
0
Two questions:

1) How can I get the error message(ls: asdf not found) in the program?

2) Why is errno 0 ?

Thanks,
Jose Luis.
Nov 14 '05 #1
5 9061
jose luis fernandez diaz <jo**********************@yahoo.es> scribbled the following
on comp.lang.c:
Hi, In the program below: #include <stdio.h>
#include <stdlib.h>
#include <errno.h> using namespace std;
int main()
{
system("ls asdf"); printf("%d\n", errno); return 0;
}
the output is: ls: asdf not found
0
Two questions: 1) How can I get the error message(ls: asdf not found) in the program?
By redirecting it to a file and reading from there. It's the only way
supported by ISO standard C or C++.
2) Why is errno 0 ?


Because the program was correctly able to call the command ls. The fact
that the command ls itself failed is none of C's or C++'s concern.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
Nov 14 '05 #2
On Fri, 14 May 2004, jose luis fernandez diaz wrote:
Hi,

In the program below:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

using namespace std;
int main()
{
system("ls asdf");
printf("%d\n", errno);
return 0;
}

the output is:

ls: asdf not found
0

Two questions:

1) How can I get the error message(ls: asdf not found) in the program?
Cannot give a definitive answer using just ISO/ANSI C. You need to get
into system specific code. Something like:

system("ls asdf > error.txt");

might work. But not if > only captures stdout and the message is sent to
stderr. The type of shell that system is using will make a difference. You
might need to use:

system("ls asdf 2> error.txt");
or
system("ls asdf >& error.txt");

The possibilities are many. It will depend on so many factors. Namely, how
is system() implemented? How is the command implemented? how is the system
configured?
2) Why is errno 0 ?


Why not? There is nothing in the system() command that indicates it will
set errno for any reason. I could run a program as a user where system()
fails and it still won't set errno to anything different.

You are going to have to restrict your solution to a specific operating
system. In which case you need to ask a newsgroup that deals with your
operating system to find out the best solution.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3
In <c2**************************@posting.google.com > jo**********************@yahoo.es (jose luis fernandez diaz) writes:
In the program below:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

using namespace std;
This is a syntax error in C. If you cross-post, make sure that your code
is correct in both languages!
int main()
{
system("ls asdf");

printf("%d\n", errno);

return 0;
}

the output is:

ls: asdf not found
0
Two questions:

1) How can I get the error message(ls: asdf not found) in the program?
By not using system() in the first place. Your platform provides a better
alternative. It's even documented in the c.l.c FAQ, which you didn't
bother to check *before* posting.
2) Why is errno 0 ?


Why not? The C standard doesn't require system() to touch errno at all
and your platform's documentation says:

ERRORS

The system() function may set errno values as described by fork().
In addition, system() may fail if:

[ECHILD]
The status of the child process created by system()
is no longer available.

The implied fork() call obviously succeeded, so it had no reason to touch
errno, either.

Before posting such questions, try to understand how things work and
check the documentation. Sure, there *was* an errno set somewhere,
but this somewhere was the process executing the ls command, not *your*
program. That errno was lost without trace by the time that process
terminated, before your system() call returned.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
The way I understand it, piping output to a file via system() isn't easily
do-able portably. You might want to look into popen() or a pipe() - fork() -
exec()

--
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/

"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote in
message news:c2**************************@posting.google.c om...
Hi,

In the program below:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

using namespace std;
int main()
{
system("ls asdf");

printf("%d\n", errno);

return 0;
}
the output is:

ls: asdf not found
0
Two questions:

1) How can I get the error message(ls: asdf not found) in the program?

2) Why is errno 0 ?

Thanks,
Jose Luis.

Nov 14 '05 #5
In <1O***************@newsfe2-gui.server.ntli.net> "Kieran Simkin" <ki****@digital-crocus.com> writes:
The way I understand it, piping output to a file via system() isn't easily
do-able portably. You might want to look into popen() or a pipe() - fork() -
exec()


Redirecting output to a file via system() is a lot more portable than
popen() or pipe-fork-exec. So, if portability is an overriding issue,
output redirection is the winning approch.

OTOH, portability is seldom an issue when system() is used, considering
that system's argument is highly non-portable (when it's not a null
pointer).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6

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

Similar topics

1
by: DM | last post by:
Get this error when I try to run any ASP.NET web app from Visual Studio on my Win2K PC. Anyone seen this before ? BC32400: Class 'CLSID_CorSymWriter' could not be created: System Error...
1
by: Mark | last post by:
Not sure this is the right place for this questions, but here goes: I get an error message when deleting an table from a Access database. The code is as follows and the error message is after...
4
by: Mark | last post by:
Not sure this is the right place for this questions, but here goes: I get an error message when deleting an table from a Access database. The code is as follows and the error message is after...
5
by: jose luis fernandez diaz | last post by:
Hi, In the program below: #include <stdio.h> #include <stdlib.h> #include <errno.h> using namespace std; int main()
9
by: JTrigger | last post by:
When I compile my project using the IDE on a development machine it works just fine. When I compile it on the server using csc.exe, I get the following error when I try to bring it up in the web...
5
by: Bruce Schechter | last post by:
I just started to develop an ASP.NET application in vs.net 2003 . But each time I try to execute the application (which is basically empty so far), I get a dialog box titled "Microsoft Development...
4
by: james margey | last post by:
Hi to all, I have spent 3 days at this error and i have two days to go for a deadline, and i am about to go off my nut, the reason being: Microsoft dont seem to be able to provide a solution, I...
5
by: Kenneth P | last post by:
Hi, I get a Parser Error message when I try to start my asp.net app. Parser Error Message: Could not load type 'DiskoWeb.dLogin'. I have a code behind file and it's class name is dLogin. ...
7
by: deltalimagolf | last post by:
I now get the following error message after copying an asp.net application to the deployment web server. I don't have SQLExpress or any version of SQL 2005 installed. I found the "LocalSQLServer"...
1
by: Bruce | last post by:
We suddenly got a mysterious error message on our Windows Server 2003 (SP1) after rolling out a bug fix on a web services application this morning. The application compiles and runs fine on the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.