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