简体   繁体   中英

search and replace text

I am writing a small C program that searches for a string of text within a file and replaces it with another string but while doing this I keep getting a segmentation fault and for some reason my buffer (named c) is empty after my fgets call.

here is my code:

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



/*
 *program replaces all strings that match a certain pattern within a file
 */

int main(int argc, char** argv)
{
    // check if there are correct amount of arguments
    if(argc != 4) 
    {
            printf("Error, incorrect amount of input arguments!\n");
            return 1;
    } // end if

    // initializers
    int i;
    char* temp;
    FILE* searchFile;
    char* c = malloc(sizeof(char));
    char* fileName = malloc(sizeof(argv[1]));
    char** searchWord = malloc(sizeof(argv[2]));
    char* replaceWord = malloc(sizeof(argv[3]));

    fileName = argv[1];
    *searchWord = argv[2];
    replaceWord = argv[3];

    // checks to see if searchWord isnt too big
    if(strlen(*searchWord) > 256)
    {
            printf("Error, incorrect amount of input arguments!\n");
            return 1;
    }

    // opens file
    searchFile = fopen(fileName,"r+");

    // searches through file
    do
    {
            fgets(c, 1, searchFile);

            i = 0;
            while(i < strlen(*searchWord))
            {
                    printf("search character number %i: %c\n", i, *searchWord[i]);     

                    /* 
                     * finds number of letters in searchWord 
                     * by incrementing i until it is equal to size of searchWord
                     */
                    if(strcmp(c,searchWord[i])) 
                    {

                            i++;
                    }

                    // replaces searchWord with replace word
                    if(i == (strlen(*searchWord)))
                    {
                            printf("inside replace loop\n");
                            memcpy(searchWord, replaceWord,(sizeof(replaceWord)/sizeof(char))+1);
                            printf("The search term (%s) has been replaced with the term: %s!\n",*searchWord,replaceWord);
                    }
            }
    }while(strlen(c) > 0);

    // closes file
    fclose(searchFile);
}

You are passing a size of 1 to fgets. But the fgets function reads at most one less than the number of characters specified by the size from the given stream. So if you pass a size of 1 it reads 0 characters. The reason it reads one less is so that there is room left for the null 'end of line' character.

The fgets function stops reading when a newline character is found, at the end of the file or on an error and the newline, if any, is retained. So you need to malloc c to be however many characters you expect to be in the string plus one for the newline and one for the null 'end of line' character.

A couple other things to note. The first of the following two statements first allocate space to store a filename and then point the filename pointer to it. The second then points the filename pointer to point to the passed in string containing the first arguement to the program:

char* fileName = malloc(sizeof(argv[1]));
fileName = argv[1];

Either just point the filename pointer there directly and don't allocate any memory:

char* fileName = argv[1];

Or, if you actually need allocated memory, change the second line to copy the contents of the string:

char* fileName = malloc(sizeof(argv[1]));
strcpy(fileName,argv[1]);

Or, even easier use strdup to allocate the memory and then copy the contents:

char* fileName = strdup(argv[1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM