Public Group

what is wrong with my very simple codes??

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I wrote a very simple C program on linux. But I can not see the result of "ls" on screen but only "************". Who can give me a anwser to see the files list on screen? Thanks! #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> void main( void ) { char *cmd[1]= {"ls"}; execvp("ls", cmd); printf("*************"); }

Share on other sites
Not sure, but can't you do System("ls") (maybe it's a lowercase 's' in "system"). See if that works.

EDIT: Also, with system calls, the buffer is not always flushed automatically. Maybe that's your problem.

EDIT2: If I'm not mistaken, your code is calling "ls" with parameter "ls." Try making cmd "-a" or something.

Share on other sites
Why are you trying to pass in 'ls' as a parameter to ls? That, I think, would be why you are not seeing anything: you probably don't have something called 'ls' in the current directory.  Forget I said anything.
Try running System() like cowsarenotevil suggested.
By the way, your array should be something like this:
char *cmd[] = { "ls", 0 }; (null-terminated)
[edit2] I tested your code on a *nix system (SunOS 5.8, GCC 3.2.3), and the problem was that your array was not null-terminated. [edit3] Note, also, that main() should return an int.

jfl.

Share on other sites
man, he's instructing the program to print the *'s, that's why the output is a bunch of *'s. pretty logical, isn't it?

Share on other sites
Quote:
 Original post by Anonymous Posterman, he's instructing the program to print the *'s, that's why the output is a bunch of *'s. pretty logical, isn't it?

You obviously read neither his code, nor his question.

Share on other sites
Try this:

#include <sys/types.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>main(){   pid_t pid;   char *const parmList[] =    {"/bin/ls", "-l", NULL};   if ((pid = fork()) == -1)      perror("fork() error");   else if (pid == 0) {      execvp("ls", parmList);      printf("Return not expected. Must be an execvp() error.\\n");   }}

edit: removed '\' from from lines 21 and 29

[Edited by - jdaniel on November 5, 2005 6:51:42 PM]

Share on other sites
Quote:
 Original post by jdanielTry this:*** Source Snippet Removed ***

Remove the '\' from lines 21 and 29 ;).

I'm not sure why, but printf's after execvp do nothing. I don't typically program in *nix, unfortunately.  After reading through the man-pages more thoroghly, this seems to be by design. the exec* family of functions *replace* the current process with the one specified, which is why the function does not return on success :).
It works with system(), though.

Share on other sites
exec replaces the current process with another one, so it will in effect exit your program. You should use a variant of spawn, system or fork and exec.

Share on other sites
why caring, anyways? he'll never be tim sweeney or something

Share on other sites
Quote:
 Original post by jdanielTry this:*** Source Snippet Removed ***edit: removed '\' from from lines 21 and 29

these codes do work, thanks!!

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 21
• 12
• 11
• 41
• Forum Statistics

• Total Topics
631401
• Total Posts
2999870
×