The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
c system call rangaswamy High Level Programming 1 02-19-2008 10:53 AM
System hanged. help required. praveen_b744 UNIX for Advanced & Expert Users 8 09-20-2007 06:03 PM
how to differentiate system call from library call muru UNIX for Advanced & Expert Users 2 07-19-2007 08:20 PM
system() function call... Vishnu High Level Programming 1 10-15-2002 08:36 AM
URGENT Help required regarding the use of FORK system call kkumar1975 UNIX for Advanced & Expert Users 4 10-08-2001 02:33 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 03-20-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Putting Code tags and indenting the source listings makes it so much easier to follow. The statements for initializing and incrementing i and the while are unneccessary as a single for loop can replace 'em.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
main(argc,argv)
int argc;
char *argv[];
{
   int i;
   char cmd[1024];
   printf("%d is arg\n",argc);
   printf("arg0 is %s\n",argv[0]);
   printf("arg1 is %s\n",argv[1]);
   if(argc!=2)
   {
      printf("usage Wrong\n");
      exit(0);
   }
   printf("The command you ahve entered is %s\n",argv[1]);
   printf("Let us display the various outputs for different options for the command\n");
   for (i= 'a'; i <= 'z'; ++i)
   {
      printf("\n option is %d ASCII is %c\n",i,i);
      sprintf(cmd, "%s -%c", argv[1], i);
      system(cmd);
   }
}
Reply With Quote
Forum Sponsor
  #9  
Old 03-23-2008
Registered User
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 48
Wink Its working fine now.

Hi Shamrock,

Thankyou very much for the solution rendered. Its working very fine now. Sorry for the delayed reply. I wasn't logged for the past 3 days.

Thanks,
Ramkrix
Reply With Quote
  #10  
Old 03-23-2008
Registered User
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 48
Thumbs up

Hi Shamrock/fpmurphy,

I have changed the code using sprintf and is working fine and I have one more doubt with the output of the program now.

Code:

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

main(argc,argv)
int argc;
char *argv[];
{
int i;
/*char *cmd,*opt;*/
char cmd[1024];
printf("%d is arg\nc",argc);
printf("arg0 is %s\n",argv[0]);
printf("arg1 is %s\n",argv[1]);

if(argc!=2)
{
printf("usage Wrong");
exit(0);
}

printf("The command you ahve entered is %s\n",argv[1]);
printf("Let us display the various outputs for different options for the command");

i='a';
while(i>='a'&& i<='z')
{
printf("\n option is %d ASCII is %c",i,i);
sprintf(cmd,"%s -%c",argv[1],i);
system(cmd);
i++;
}

}



Output:

[ramki@lindesk3 ramki]$ ./cmd_opt ls
2 is arg
carg0 is ./cmd_opt
arg1 is ls
The command you ahve entered is ls
Let us display the various outputs for different options for the command
. .bash_history .bashrc db_cnnct_stps .gtkrc myshell sys_info xplrng_c
.. .bash_logout CCS .emacs hi.txt .mysql_history sysint_ex
ascii .bash_profile cmd_opt filename.sh .kde options_wth_exe_cmds.c .viminfo
option is 97 ASCII is a
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 98 ASCII is b
options_wth_exe_cmds.c hi.txt cmd_opt sysint_ex filename.sh db_cnnct_stps CCS xplrng_c myshell ascii sys_info
option is 99 ASCII is c
.
option is 100 ASCII is d
ls: invalid option -- e
Try `ls --help' for more information.
option is 101 ASCII is e
. .emacs .bashrc .viminfo .mysql_history cmd_opt xplrng_c CCS
.. .bash_logout .gtkrc myshell db_cnnct_stps filename.sh sys_info
.kde .bash_profile ascii .bash_history sysint_ex hi.txt options_wth_exe_cmds.c
option is 102 ASCII is f
total 52
-rw-r--r-- 1 users 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 users 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 users 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 users 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 users 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 users 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 users 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 users 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 users 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 users 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 users 4096 Mar 12 15:35 xplrng_c
option is 103 ASCII is g
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 104 ASCII is h
147745 ascii 148103 db_cnnct_stps 908434 myshell 908425 sysint_ex
908426 CCS 148232 filename.sh 148221 options_wth_exe_cmds.c 907491 xplrng_c
147812 cmd_opt 148142 hi.txt 908388 sys_info
option is 105 ASCII is i
ls: invalid option -- j
Try `ls --help' for more information.
option is 106 ASCII is j
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 107 ASCII is k
total 52
-rw-r--r-- 1 ramki users 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 ramki users 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 ramki users 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 ramki users 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 ramki users 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 ramki users 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 ramki users 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 ramki users 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 ramki users 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 ramki users 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 ramki users 4096 Mar 12 15:35 xplrng_c
option is 108 ASCII is l
ascii, CCS, cmd_opt, db_cnnct_stps, filename.sh, hi.txt, myshell, options_wth_exe_cmds.c, sys_info, sysint_ex, xplrng_c
option is 109 ASCII is m
total 52
-rw-r--r-- 1 578 100 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 578 100 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 578 100 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 578 100 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 578 100 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 578 100 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 578 100 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 578 100 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 578 100 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 578 100 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 578 100 4096 Mar 12 15:35 xplrng_c
option is 110 ASCII is n
total 52
-rw-r--r-- 1 ramki 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 ramki 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 ramki 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 ramki 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 ramki 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 ramki 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 ramki 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 ramki 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 ramki 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 ramki 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 ramki 4096 Mar 12 15:35 xplrng_c
option is 111 ASCII is o
ascii CCS/ cmd_opt db_cnnct_stps filename.sh hi.txt myshell/ options_wth_exe_cmds.c sys_info/ sysint_ex/ xplrng_c/
option is 112 ASCII is p
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 113 ASCII is q
xplrng_c sysint_ex sys_info options_wth_exe_cmds.c myshell hi.txt filename.sh db_cnnct_stps cmd_opt CCS ascii
option is 114 ASCII is r
total 52
4 ascii 8 cmd_opt 4 filename.sh 4 myshell 4 sys_info 4 xplrng_c
4 CCS 4 db_cnnct_stps 8 hi.txt 4 options_wth_exe_cmds.c 4 sysint_ex
option is 115 ASCII is s
options_wth_exe_cmds.c hi.txt cmd_opt sysint_ex filename.sh db_cnnct_stps CCS xplrng_c myshell ascii sys_info
option is 116 ASCII is t
cmd_opt options_wth_exe_cmds.c hi.txt CCS myshell sys_info sysint_ex xplrng_c ascii filename.sh db_cnnct_stps
option is 117 ASCII is u
CCS ascii cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 118 ASCII is v
ls: option requires an argument -- w
Try `ls --help' for more information.
option is 119 ASCII is w
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 120 ASCII is x
ls: invalid option -- y
Try `ls --help' for more information.
option is 121 ASCII is y
ls: invalid option -- z
Try `ls --help' for more information.
option is 122 ASCII is z


ACtually the "option is 97 ASCII is a" has to be printed first and then its output ". .bash_history .bashrc db_cnnct_stps .gtkrc myshell sys_info xplrng_c
.. .bash_logout CCS .emacs hi.txt .mysql_history sysint_ex
ascii .bash_profile cmd_opt filename.sh .kde options_wth_exe_cmds.c .viminfo
"

I experienced the same thing with another C program.. Where the unix system call [which has been called 5 times in a while loop] output is printed first and then the output of printf 5 times gets printed [printf is used preceding to the unix system vall in that particular program] . Are the unix system calls functions that much faster so that the sequence of program written is not followed?

Please explain.

Thanks,
Ramkrix
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0