Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Use of command line arguments

Posted By: Rebecca Hughes     Category: C Programming     Views: 6336

Write a program that will receive a filename and a line of text as command line arguments and write the text to the file.

Code for Use of command line arguments in C Programming

   #include  <stdio.h>                                              
                                                                    
   main(argc, argv)                                                 
   int  argc;                    /*   argument count           */
char *argv[]; /* list of arguments */
{ FILE *fp; int i; char word[15]; fp = fopen(argv[1], "w"); /* open file with name argv[1] */
printf("\nNo. of arguments in Command line = %d\n\n",argc); for(i = 2; i < argc; i++) fprintf(fp,"%s ", argv[i]); /* write to file argv[1] */
fclose(fp); /* Writing content of the file to screen */
printf("Contents of %s file\n\n", argv[1]); fp = fopen(argv[1], "r"); for(i = 2; i < argc; i++) { fscanf(fp,"%s", word); printf("%s ", word); } fclose(fp); printf("\n\n"); /* Writing the arguments from memory */
for(i = 0; i < argc; i++) printf("%*s \n", i*5,argv[i]); } Output C>F12_7 TEXT AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF GGGGG No. of arguments in Command line = 9 Contents of TEXT file AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF GGGGGG C:\C\F12_7.EXE TEXT AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE FFFFFF GGGGGG
  
Share: 


Didn't find what you were looking for? Find more on Use of command line arguments Or get search suggestion and latest updates.

Rebecca Hughes
Rebecca Hughes author of Use of command line arguments is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!