Below is a Simple Shell Program in C Language which Implement search command as follows
1) search first occurrence of pattern in file name
2) count no of occurrences of pattern in file name
3) search all occurrences of pattern in file name
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <string.h>
void main( int argc, char *argv[])
{
char pattern[20],fname[20],option;
printf("\nSearch Starts..");
if( argc != 4 )
{
printf("\nInvalid number of arguments");
return;
}// if
// set param
option = *argv[1];
strcpy(pattern,argv[2]);
strcpy(fname,argv[3]);
switch(option)
{
case 'f' : find_first_occ(fname,pattern);
break;
case 'c' : count_all_occ(fname,pattern);
break;
case 'a' : print_all_occ(fname,pattern);
break;
default : printf("\nInvalid option");
break;
} // switch
} // main
int find_first_occ( char fname[], char pattern[])
{
FILE *fp;
char str[100],*ptr;
int i,m,c,lineno;
fp = fopen(fname,"r");
if( fp == NULL )
{
printf("\nUnable to open given file");
return;
}
lineno=0;
while ( !feof(fp))
{
strset(str,'\0');
fgets(str,99,fp);
lineno++;
ptr = strstr(str,pattern);
if( ptr != NULL )
{
printf("\n lineno = %d : Line = %s", lineno,str);
printf("\n position = %d", ptr - str +1 );
break;
}
} // while
fclose(fp);
return;
} // find first occ
int count_all_occ( char fname[], char pattern[])
{
FILE *fp;
char str[100],*ptr,*ptr1;
int count=0;
int i,m,c,lineno;
fp = fopen(fname,"r");
if( fp == NULL )
{
printf("\nUnable to open given file");
return;
}
while ( !feof(fp))
{
strset(str,'\0');
fgets(str,99,fp);
ptr1 = str;
ptr = strstr(ptr1,pattern);
while( ptr != NULL )
{
count++;
ptr1= ptr+1;
ptr = strstr(ptr1,pattern);
}
} // while
fclose(fp);
printf("\nPattern %s occurs %d times", pattern,count);
return;
} // count all occ
int print_all_occ( char fname[], char pattern[])
{
FILE *fp;
char str[100],*ptr,*ptr1;
int lineno=0;
int i,m,c;
fp = fopen(fname,"r");
if( fp == NULL )
{
printf("\nUnable to open given file");
return;
}
while ( !feof(fp))
{
strset(str,'\0');
fgets(str,99,fp);
lineno++;
if( strstr(str,pattern) != NULL )
printf("Line:%d: %s",lineno,str);
} // while
fclose(fp);
return;
} // print all occurences
1) search first occurrence of pattern in file name
2) count no of occurrences of pattern in file name
3) search all occurrences of pattern in file name
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <string.h>
void main( int argc, char *argv[])
{
char pattern[20],fname[20],option;
printf("\nSearch Starts..");
if( argc != 4 )
{
printf("\nInvalid number of arguments");
return;
}// if
// set param
option = *argv[1];
strcpy(pattern,argv[2]);
strcpy(fname,argv[3]);
switch(option)
{
case 'f' : find_first_occ(fname,pattern);
break;
case 'c' : count_all_occ(fname,pattern);
break;
case 'a' : print_all_occ(fname,pattern);
break;
default : printf("\nInvalid option");
break;
} // switch
} // main
int find_first_occ( char fname[], char pattern[])
{
FILE *fp;
char str[100],*ptr;
int i,m,c,lineno;
fp = fopen(fname,"r");
if( fp == NULL )
{
printf("\nUnable to open given file");
return;
}
lineno=0;
while ( !feof(fp))
{
strset(str,'\0');
fgets(str,99,fp);
lineno++;
ptr = strstr(str,pattern);
if( ptr != NULL )
{
printf("\n lineno = %d : Line = %s", lineno,str);
printf("\n position = %d", ptr - str +1 );
break;
}
} // while
fclose(fp);
return;
} // find first occ
int count_all_occ( char fname[], char pattern[])
{
FILE *fp;
char str[100],*ptr,*ptr1;
int count=0;
int i,m,c,lineno;
fp = fopen(fname,"r");
if( fp == NULL )
{
printf("\nUnable to open given file");
return;
}
while ( !feof(fp))
{
strset(str,'\0');
fgets(str,99,fp);
ptr1 = str;
ptr = strstr(ptr1,pattern);
while( ptr != NULL )
{
count++;
ptr1= ptr+1;
ptr = strstr(ptr1,pattern);
}
} // while
fclose(fp);
printf("\nPattern %s occurs %d times", pattern,count);
return;
} // count all occ
int print_all_occ( char fname[], char pattern[])
{
FILE *fp;
char str[100],*ptr,*ptr1;
int lineno=0;
int i,m,c;
fp = fopen(fname,"r");
if( fp == NULL )
{
printf("\nUnable to open given file");
return;
}
while ( !feof(fp))
{
strset(str,'\0');
fgets(str,99,fp);
lineno++;
if( strstr(str,pattern) != NULL )
printf("Line:%d: %s",lineno,str);
} // while
fclose(fp);
return;
} // print all occurences
No comments:
Post a Comment