LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2007, 02:38 PM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Rep: Reputation: 30
Executing a program in c


Hi,

I am writing a program in c that needs to execute another program and continues with the code that follows afterwards.

I have tried to achieve it with the following code but it doesn't work:

Code:
#include <stdio.h>
#include <unistd.h>


main()
{

char *cmd[] = { "ls", "-l", (char *)0 };
execv ("/bin/ls", cmd);

char continuing[] = "continuing";
printf("%s", continuing);


return 0;


}
Can someone tell me how I could do this?

Thanks
 
Old 07-27-2007, 03:13 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here's one alternative:
Code:
#include <stdio.h>
#include <unistd.h>

void
main(int argc, char *argv[])
{
  char cmd[80];

  /* Check command line arg */
  if (argc != 2)
  {
    printf ("USAGE: myprog SOMEFILE\n");
    return 1;  /* Exit the program: Return "Not OK" */
  }

  /* Generate the command to execute, using the command-line parameter "argv[1]" */
  sprintf (cmd, "ls -l %s", argv[1]};

  /* Execute the command */
  system (cmd);

  printf("continuing...\n");
  return 0; /* Exit the program: Return "OK" */
}
 
Old 07-27-2007, 03:34 PM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks paulsm4
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program on start up andrewthk Debian 3 03-23-2006 12:49 AM
How executing c program with php? payal_shah Programming 6 04-08-2005 10:44 PM
executing a program in new terminal? kashifbilal Programming 7 04-26-2004 04:52 AM
Servlets executing a C program. webee Programming 1 02-04-2003 05:19 PM
Executing a program with out system() in c++ sobdk Programming 2 11-02-2002 07:45 AM


All times are GMT -5. The time now is 01:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration