危险的脚本它会起作用吗?

时间:2014-08-26 03:52:58

标签: c file-io replace

我正在尝试创建一个打开文本文件并将其内容char-by-char读取到最后的函数,当函数遇到特定字符串的匹配时,它会清除字符串所在的行。 (或者至少用空字符替换每个字符)。

这就是我所拥有的:

void clear_blocker ()
{
    FILE* fp = fopen("debug_output.txt", "r+"); // opens a file to read and update
    char handle [] = // the string to compare from
    "Critical: Unexpected Crash Event. Application forced to terminate/task killed";
    char buff[] = // allocating the buff statically. lazy counting.
    "Critical: Unexpected Crash Event. Application forced to terminate/task killed+";
    long cur, n=0, iter;

    fseek(fp, 0L, SEEK_SET); // secure rewinding
    *buff = NULL; // emptying buff content
    for( ; cur != EOF; cur=fgetc(fp)) // loop trough the file, obtaining each char
    {
        n++; // counter
        if(strlen(buff) <= strlen(handle))  buff[n] = cur; // copying strings (size-limited)
        else { *buff = NULL; n = 0; } // restarts content of buff to obtain next
        if(!strcmp(buff, handle)) // if the content matches the string
        {
            for(iter = ftell(fp)-strlen(handle); iter<ftell(fp); iter++) // iter indicates the start of the string in the file and it increments to the end
            {
                fseek(fp, iter, SEEK_SET); // using the iter to set the position indicator
                fputc(NULL, fp); // ..and put NULL there
            }
            break; // breaks out the loop
        }
    }

}

我的代码问题在于它说我在该行上溢出了数组buff

if(strlen(buff) <= strlen(handle))  buff[n] = cur;

我相信它还有更多问题。

1 个答案:

答案 0 :(得分:-1)

我写了一些strstr函数,因为我没有启用它。我希望它与真实的相似,它会有效。

char* strstrr ( char *str1, char *str2 )
{
    int i
    char* left = malloc(strlen(str1)+strlen(str2)+1);

    for(i=0; i<strlen(str1); i++)
    {
        strcpy(left, str2);
        strcat(left, &str1[i+strlen(str2)]);

        if(!strcmp(&str1[i], left)) { return &str1[i]; }
    }
    free(left);
    return NULL;
}