Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 什么';我的函数有问题(从文本文件中删除字符串)_C - Fatal编程技术网

C 什么';我的函数有问题(从文本文件中删除字符串)

C 什么';我的函数有问题(从文本文件中删除字符串),c,C,我想写一个函数,从文本文件中删除某些单词。程序运行良好,但valgrind说了一些不同的东西: ==3411== Source and destination overlap in strcpy(0x51f1c90, 0x51f1c92) ==3411== at 0x4C2C085: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==3411== by 0x400AD7: DELTEword (remo

我想写一个函数,从文本文件中删除某些单词。程序运行良好,但valgrind说了一些不同的东西:

==3411== Source and destination overlap in strcpy(0x51f1c90, 0x51f1c92)
==3411==    at 0x4C2C085: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3411==    by 0x400AD7: DELTEword (remove2.c:113)
==3411==    by 0x4009A1: main (remove2.c:73)
此外,当我试图删除例如:“去”这个词时,有时“ro”这个词也会被删除。为什么?

这是我的密码:

int DELTEword(char *word, char *KEYword)
{
    int i, k = 0, l = 0, length;
    char *ptr;

    if(word != NULL)
    {
        length = strlen(KEYword);
        for(i = 0; word[i] != '\0'; i++)
        {
            if(word[i] == KEYword[k])
            {
                l++;
                k++;
            }

            if(l == length)
            {
                ptr = &word[i];
                strcpy((ptr - length) + 1, ptr + 1);
                l = 0;
                k = 0;
            }
        }
        return 1;
    }
    else return 0;
 }

使用
memmove
而不是
strcpy
来洗牌数据,因为如果源和目标重叠,则不建议使用
strcpy
memmove
可以安全地在重叠的情况下使用。

使用
memmove
而不是
strcpy
来移动数据,因为如果源和目标重叠,则不建议使用
strcpy
memmove
在重叠的情况下使用是安全的。

我认为问题在于下面的代码

for(i = 0; word[i] != '\0'; i++)
{
    if(word[i] == KEYword[k])
    {
        l++;
        k++;
    }
}
它不寻找连续的字母。如果字母不匹配,则将从下一个字母继续。我的意思是
else
零件处理缺失

也许这有帮助。这匹配所有发生的事件,我是指
good
中的单词
go
。可以根据您的需要进行调整

length = strlen(KEYword);
for(i = 0; word[i] != '\0'; i++)
{
    k = i;
    l = 0;
    for (j = 0; j < length; j++) {
        if(word[k] == KEYword[j])
        {
            l++;
            k++;
        }
        else
        {
            break;
        }
    }
    /* All letters matched */
    if (l == length) {
        /* do some stuff */
    }
}
length=strlen(关键字);
for(i=0;单词[i]!='\0';i++)
{
k=i;
l=0;
对于(j=0;j
我认为问题在于以下代码

for(i = 0; word[i] != '\0'; i++)
{
    if(word[i] == KEYword[k])
    {
        l++;
        k++;
    }
}
它不寻找连续的字母。如果字母不匹配,则将从下一个字母继续。我的意思是
else
零件处理缺失

也许这有帮助。这匹配所有发生的事件,我是指
good
中的单词
go
。可以根据您的需要进行调整

length = strlen(KEYword);
for(i = 0; word[i] != '\0'; i++)
{
    k = i;
    l = 0;
    for (j = 0; j < length; j++) {
        if(word[k] == KEYword[j])
        {
            l++;
            k++;
        }
        else
        {
            break;
        }
    }
    /* All letters matched */
    if (l == length) {
        /* do some stuff */
    }
}
length=strlen(关键字);
for(i=0;单词[i]!='\0';i++)
{
k=i;
l=0;
对于(j=0;j