c - 搜索和替换文本

标签 c parsing search replace fopen

我正在编写一个小的 C 程序,它在文件中搜索文本字符串并将其替换为另一个字符串,但是在执行此操作时我不断遇到段错误,并且由于某种原因我的缓冲区(名为 c)在我之后是空的fgets 调用。

这是我的代码:

#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);
}

最佳答案

您正在将大小 1 传递给 fgets。但是 fgets 函数最多从给定流中读取比 size 指定的字符数少一个。因此,如果您传递 1 的大小,它会读取 0 个字符。它少读取一个的原因是为空的“行尾”字符留有空间。

当发现换行符、文件末尾或出现错误时,fgets 函数将停止读取,并保留换行符(如果有)。因此,您需要 malloc c 为您希望在字符串中包含的字符数加上一个用于换行符和一个用于空“行尾”字符。

还有一些需要注意的事情。以下两个语句中的第一个首先分配空间来存储文件名,然后将文件名指针指向它。第二个然后将文件名指针指向包含程序第一个参数的传入字符串:

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

要么直接将文件名指针指向那里并且不分配任何内存:

char* fileName = argv[1];

或者,如果您确实需要分配内存,请更改第二行以复制字符串的内容:

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

或者,更容易使用 strdup 分配内存,然后复制内容:

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

关于c - 搜索和替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13023393/

相关文章:

C 从最初分配的数组中引用静态内存

c - 二维动态数组重新分配

parsing - "Sub-parsers"在管道-attoparsec

regex - 我可以对索引子文档字段执行 MongoDB "starts with"查询吗?

c - 一些组装说明

c - C 中的链表和指针

r - 正确解析 R 中的 "formula"对象

php - PHP解析/语法错误;以及如何解决它们

mysql - 如何从另一列的一列中搜索字符串并删除?

mysql - 在 MySql 中快速搜索邮政编码