13 Apr 2013

Command Line arguments

//Command Line Arguments 
 
#include <stdio.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int x;
            /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
 
 
TO KNOW mORE....http://courses.cms.caltech.edu/cs11/material/c/mike/misc/cmdline_args.html 

Categories: , , , ,

Related Posts:

  • Transpose matrix /* program to find a transpose of a matrix..... */ #include <stdio.h> int main() { int a[10][10], trans[10][10], r, c, i, j; printf("Enter rows and column of matrix: "); scanf("%d %d", &r, &… Read More
  • Fire Escape Routes_solved /* CODECHEF Problem code: FIRESC */ #include<stdio.h> inline int min(int a,int b) { if (a<b) return a; else return b; } int t,n,m,x,y,i,j,id[100002],sz[100002],count,ng; int find(int p) { … Read More
  • Reverse string /* program to reverse a string.... */ #include<iostream.h>#include<conio.h>#include<stdio.h>#include<stdlib.h>#include<string.h>  void main(){clrscr();  char a[80];int i,k;  … Read More
  • COMPARE strings /* program to compare two strings.... */ #include<iostream.h>#include<conio.h>#include<stdio.h>#include<string.h>  void main(){  clrscr();  char a[50],b[50];int i,j,k=0;  cout… Read More
  • Reverse number /* program to reverse a number..... */  #include<stdio.h>#include<conio.h> void main (){  long int num,mod,rev=0;  printf("Enter a number:");scanf("%ld", &num);while (num>0){mod=num%10… Read More

0 comments:

Post a Comment

Copyright © 2025 UPgradeCODING | Powered by Blogger