Hi!
My code is to search for the strings. Example when you type srtool.exe -s in -f sample.txt (in the command prompt), all the words containing "in" will be changed to ***in***
My problem is all words containing in will change to ** (one asterisk per word) instead of ***in***
please help me to look for the problem. i badly need the code by tom. Thanks!:)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define SIZE 1000
void print_err();
FILE* fp=NULL;
FILE* fp_out=NULL;
int main (int argc, char *argv[])
{
char infile[SIZE];
char outfile[SIZE];
char search_string[SIZE];
char replace_string[SIZE];
int f_count=0, r_count=0, o_count=0, s_count=0, i_count=0, w_count=0;
int search_str_len;
int i;
//checking the argument from the command prompt
for(i=1; i<argc; i++) {
if(strlen(argv[i])!=2)
print_err();
if(argv[i][0]!='-')
print_err();
switch (argv[i][1]) {
case 's':
++s_count;
if(!argv[i+1])
print_err();
strcpy(search_string,argv[i+1]);
++i;
break;
case 'f':
++f_count;
if(!argv[i+1])
print_err();
strcpy(infile,argv[i+1]);
++i;
break;
case 'o':
++o_count;
if(!argv[i+1])
print_err();
strcpy(outfile,argv[i+1]);
++i;
break;
case 'r':
++r_count;
if(!argv[i+1])
print_err();
strcpy(replace_string,argv[i+1]);
++i;
break;
case 'i':
++i_count;
break;
case 'w':
++w_count;
break;
default:
print_err();
break;
}
}
char new_word[SIZE];
char line[SIZE];
FILE* fp = fopen(infile, "r");
FILE* fp_out = fopen(outfile, "w");
while(fgets(line, SIZE, fp)){
char * p = strstr(line, search_string);
strcpy(new_word,"***");
strcat(new_word, search_string);
strcat(new_word, "***");
while (p != NULL) {
strncpy (p, new_word, strlen(search_string));
p = strstr (p, search_string);
}
printf("%s", line); // writes these files to the OUTFILE
fflush(stdin);
}
}
void print_err() {
printf("USAGE: srtool.exe [-s <search word>][-f <filename>] [-r <replace word>] [-w] [-i] [-o <outfile>]");
exit(1);
}