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:

  • Poker_SOLVED /* CODECHEF PROBLEM CODE : POKER  */ #include<stdio.h> int main() { int cases; int i,j; int temp; int rank[5]; char suit[5],space,ch;  scanf("%d%c",&cases,&sp… Read More
  • Money Transformation_solved /* CODECHEF PROBLEM CODE : MONTRANS */ #include<stdio.h>   int T,a,b,c; void solve() { int step=0,cnt=0; int m=a*100+b; while((a>0||b>=c)&&cnt<10000){ cnt++; … Read More
  • Chef Teams_solved /* CODECHEF PROBLEM CODE : CTEAMS */  #include <stdio.h> #include <math.h> #define tausch(a,b) b=a+b, a=b-a, b=b-a int X[100100][5]; void f3(int i) { int l=2*i, r=2*i+1, z=i; z=(l<=X[2][4… Read More
  • Multiply Matrix /* program to multiply two matrices... */ #include<iostream.h>#include<conio.h>  void main(){  clrscr();int x[50][50],y[50][50],z[50][50],i,j,k,a,b,c,d,e,f,g,h;  cout<<"Enter the rows an… Read More
  • Random decreasing function_SOLVED /* CODECHEF PROBLEM CODE : RDF */ #include <stdio.h> inline int getn()  {   int n=0, c=getchar_unlocked();   while(c < '0' || c > '9')        c = getchar_unlocke… Read More

0 comments:

Post a Comment

Copyright © 2025 UPgradeCODING | Powered by Blogger