Searching a pattern C command line argument program skeleton

 

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: Demonstrating a very simple pattern matching based on the text input from standard input

To show: Searching a pattern C command line argument program skeleton

 

 

 

// searchpattern.cpp, just a program skeleton

#include <stdio.h>

#include <string.h>

 

#define MAXLINE 100

 

int getline(char *line, int max)

{

printf("In getline() function\n");

// put getting line of text codes here

return 0;

}

 

// find and print lines that match pattern from the 1st argument

main(int argc, char *argv[])

{

char line[MAXLINE];

int found = 0;

 

// If just program name (argc = 1), then...

if(argc != 2)

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

// If the program name with switch/option (argc = 2), then...

else

while(getline(line, MAXLINE) > 0)

{

if(strstr(line, argv[1]) != NULL)

{printf("%s", line);

found++;}

}

return found;

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\searchpattern\debug>searchpattern

Usage: searchpattern thepattern

F:\vc2005project\searchpattern\debug>searchpattern mywordpattern

In getline() function

F:\vc2005project\searchpattern\debug>

 

 

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