Command line arguments – Theory and Programs [Learn to code]

What are Command line arguments? Learn syntax for using Command line arguments, programs on Command line arguments.

  • 101. Write a program accept a file name from user and print content of that file
    #include<stdio.h>
    int main(int argc, char *argv[])
    {
        if ( argc != 2 ){
             printf( "usage: %s filename", argv[0] );
        }
        else 
        {
            FILE *file = fopen( argv[1], "r" );
     
            if(file == NULL)
                 printf("Could not open filen");
            else 
            {
                int x;
                while((x=fgetc(file)) != EOF)
                     printf( "%c", x );
                fclose( file );
            }
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *