Hi!
I'm just having a problem in displaying strings of line in the command prompt. My program is to search a string and enclose it with three asterisks both sides. Though it's functioning... But the problem is the program show only those lines where the strings are present. Thr program escapes those lines which don't have.
Can you tell me the problem of my program? thanks!:icon_smile:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define SIZE 10000

void print_err();

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];
    char finalline[SIZE];
    char q[SIZE];
    int counter;
    int firstlen, nextlen, newwordlen, plen, qlen;
    int linelength, searchlength, wordlength, length, length2;

    FILE* fp = fopen(infile, "r");  //opens the specified files
    FILE* fp_out = fopen(outfile, "w");

	
	while(fgets(line, SIZE, fp)){
        char * p = strstr(line, search_string); //search for the occurrence of the search string
        wordlength = strlen(search_string);
        strcpy(new_word,"***");
        strcat(new_word, search_string);
        strcat(new_word, "***");

        while (p != NULL) {

            p = strstr (p, search_string);
            linelength = strlen(line);
            searchlength = strlen(p);
            firstlen = linelength - searchlength;

            strncat(finalline, line, firstlen);

            newwordlen = strlen(new_word);
            strncat(finalline, new_word, newwordlen);
            length = searchlength + wordlength;
            memcpy(q, &p[wordlength], length);

            p = strstr(q, search_string);

                for(counter = 0; p != NULL; counter++) {
                    plen = strlen(p);
                    qlen = strlen(q);
                    nextlen = qlen - plen;
                    strncat(finalline, q, nextlen);
                    strcat(finalline, new_word);

                    length2 = plen + wordlength;
                    memcpy(q, &p[wordlength], length);

                    p = strstr(q, search_string);
                }
            strcat(finalline, q);
        }
    }

        printf("%s", finalline); // 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);
}

Recommended Answers

All 3 Replies

You only perform the copying steps if the search string is found. Add a "not found" case where the line is copied as-is if the search string isn't located.

This is the way I will open my program:
srtool.exe -s <word> -f<file name> -r <word> -o <file name> etc...
now. all (with dash letter) are optional except for -s. How can i save a file to a different file name using -o? what if i don't want to use it (since it is optional)? i used stdout but there's an error. so i used another code but there is still an error. can you help me with this? what code can i use?

>what if i don't want to use it (since it is optional)?
Provide a suitable default. For example, with GCC if you don't provide the executable name, a.out is used as the default.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.