The main() function and its command line arguments and switches working program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: Run at command line

To do: Searching a very simple text pattern read from the command line and write to the standard output

To show: The main() function and its command line arguments and switches working C program example

 

// searchpattern.cpp, another working example

#include <stdio.h>

#include <string.h>

 

// maximum input line length

#define MAXLINE 100

 

int getline(char line[], int max)

{

int count, i;

i=0;

 

while(--max > 0 && (count=getchar()) != EOF && count != '\n')

line[i++] = count;

 

if(count=='\n')

line[i++] = count;

 

line[i]='\0';

return i;

}

 

// find all line of text matching pattern supplied by command line argument

main(int argc, char *argv[])

{

char line[MAXLINE];

long linenum = 0;

int theoption, except = 0, number = 0, found =0;

 

// check the minus sign....

while(--argc > 0 && (*++argv)[0] == '-')

// check the option...

while(theoption = *++argv[0])

switch (theoption)

{

case 'v':

except = 1;

break;

case 'n':

number = 1;

break;

default:

printf("searchpattern: illegal option %s\n", theoption);

argc = 0;

found = -1;

break;

}

//

if(argc != 1)

{

//some help...

printf("Usage: searchpattern -v -n thepattern\n");

printf("Try: searchpattern -v -n test\n");

printf("Try: searchpattern -v test\n");

printf("Try: searchpattern -n test\n");

printf("Try: searchpattern -vn test\n");

printf("Then enter line of text, with or w/o the pattern!\n");

}

//

else

while(getline(line, MAXLINE) > 0)

{

linenum++;

if((strstr(line,*argv) != NULL) != except)

{

if(number)

printf("%ld:", linenum);

printf("%s", line);

found++;

}

}

return found;

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\searchpattern\debug>searchpattern

Usage: searchpattern -v -n thepattern

Try: searchpattern -v -n test

Try: searchpattern -v test

Try: searchpattern -n test

Try: searchpattern -vn test

Then enter line of text, with or w/o the pattern!

 

 

F:\vc2005project\searchpattern\debug>searchpattern -v testpattern

The pattern to be detected is testpattern

do you see the pattern?

do you see the pattern?

Well if there is a pattern it will output it

Well if there is a pattern it will output it

else no output lor!

else no output lor!

mething like this line that having testpattern

^Z

F:\vc2005project\searchpattern\debug>

 

 

F:\vc2005project\searchpattern\debug>searchpattern

Usage: searchpattern -v -n thepattern

Try: searchpattern -v -n test

Try: searchpattern -v test

Try: searchpattern -n test

Try: searchpattern -vn test

Then enter line of text, with or w/o the pattern!

 

F:\vc2005project\searchpattern\debug>searchpattern -n mypattern

Just learning about command line arguments

The previous line don't have mypattern

2:The previous line don't have mypattern

This line will have 'mypattern' pattern

3:This line will have 'mypattern' pattern

If the mypattern is in the string it will print it out

4:If the mypattern is in the string it will print it out

else program just skip for next input

Something like the previous input that having no mypattern

6:Something like the previous input that having no mypattern

^Z

F:\vc2005project\searchpattern\debug>

 

 

F:\vc2005project\searchpattern\debug>searchpattern

Usage: searchpattern -v -n thepattern

Try: searchpattern -v -n test

Try: searchpattern -v test

Try: searchpattern -n test

Try: searchpattern -vn test

Then enter line of text, with or w/o the pattern!

 

F:\vc2005project\searchpattern\debug>searchpattern -vn thispattern

Thispattern is the pattern

1:Thispattern is the pattern

If occur in the input line it will print it out

2:If occur in the input line it will print it out

We learn about command line switches or options

3:We learn about command line switches or options

The switches or option that you use with command line

4:The switches or option that you use with command line

It is an array and pointer

5:It is an array and pointer

F:\vc2005project\searchpattern\debug>

 

 

C and C++ Programming Resources | C & C++ Code Example Index