首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的代码不能正确地用一个单词替换另一个单词

我的代码不能正确地用一个单词替换另一个单词
EN

Stack Overflow用户
提问于 2019-01-21 06:38:29
回答 2查看 56关注 0票数 0

我只是用c写了一个简单的代码,它应该从文件中提取一个文本,并用一个单词替换另一个单词。但是,我不知道为什么,我的代码只是替换了从第二个字母开始的单词。我做错了什么?这是我的代码:

代码语言:javascript
复制
 #include <stdio.h>
 #include <stdlib.h>

 int main()
 {



FILE *f;

char sir[20];

if ((f=fopen("fis.txt","r"))==NULL)
{
    printf("Not ok");
    exit(1);
}
gets(sir);
fscanf(f, "%s",sir);

printf("Give the word you are looking for and the word to replace it with");
getchar();

char s1[10],s2[10];
gets(s1);
gets(s2);


char *p, aux[100];
while (p=strstr(sir,s1))

{
    strcpy(aux,p+strlen(s1));
    strcpy(p,s2);
    strcpy(p+strlen(s2),aux);
    puts(sir);


}


}
EN

回答 2

Stack Overflow用户

发布于 2019-01-21 07:50:14

我发现你的方法有点太复杂了,只需移动指针就可以简单得多。以下是一个粗略的(1)草图:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
  /* Input file */
  FILE *f;
  /* Buffer for content of input-file */
  char sir[100] = { 0 };
  /* Temporary memory to hold the result */
  /* char tmp[100] = {0}; */
  /* Memory for the word to find and the one to replace */
  char s1[10], s2[10];
  /* Pointer to the occurrence of the word to replace */
  char *p;
  /* Pointer to sir, the memory holding the content of the file */
  char *c;

  if ((f = fopen("fis.txt", "r")) == NULL) {
    printf("Not ok");
    exit(EXIT_FAILURE);
  }
  /* Read content of file, leave room for the final `\0` */
  /* TODO: check return of fread() */
  fread(sir, 99, 1, f);

  printf("Give the word you are looking for and the word to replace it with: \n");
  /* TODO: check return of scanf() */
  /* HINT: you should read the two words separately. Ask for the word to find first,
   * read it and repeat that for the word to replace. */
  scanf("%9s %9s", s1, s2);
  /* Give user a change to stay in control. */
  printf("You are looking for %s and want it to be replaced with %s\n", s1, s2);

  /* We want to move through the input, we can do it quite comfortably with a pointer */
  c = sir;
  /* For every occurrence of the word to replace */
  while ((p = strstr(c, s1)) != NULL) {
    /* Print all characters up to the pointer p */
    /* TODO: change it to fill tmp instead. */
    /* HINT: I would use a pointer to tmp to do it but check the length! */
    while (c < p) {
      printf("%c", *c);
      c++;
    }
    /* Print the replacement / fill tmp */
    printf("%s", s2);
    /* Move the pointer to sir to the point in sir after the original word */
    c = p + strlen(s1);
  }
  /* Print / fill tmp with the rest of sir. Check the length if you use tmp! */
  printf("%s", c);
  /* Get outta here! */
  exit(EXIT_SUCCESS);
}
票数 0
EN

Stack Overflow用户

发布于 2019-01-21 15:59:51

继续我的评论,永远不要使用gets(),它是可怕的不安全的,并且容易被缓冲区溢出所利用。相反,对于面向行输入,请使用fgets或POSIX getline

在读取文本文件作为输入时,99%的情况下使用面向行的输入函数是正确的。它在这里可以很好地工作,但您必须确保您的行缓冲区足以容纳您预期的最大行。

您也不应该尝试修改您正在“就地”搜索的文件。虽然要查找和替换的单词的长度可能完全相同,但您必须小心,否则会损坏文件。写到一个新的文件,你可以自由地让你的‘查找’和‘替换’的词具有不同的长度。

假设您的单词将完全包含在您读取的行中(并且不使用连字符或以其他方式拆分多行),您可以简单地读取每一行,将指针分配给行中的起始字符,然后沿行移动指针,当字符与查找字中的字符匹配时保留索引,并在字符不同时输出替换。您必须考虑如何处理重置索引和处理部分匹配,但这只是一个简单的算术运算。

例如,如果您有一个指向打开的文件流(例如fp)的指针,该指针将每行内容读取到buf中,您的单词将在find中查找,而替换字符串将在repl中查找,则可以执行类似以下操作:

代码语言:javascript
复制
    lfind = strlen (find);                  /* length of replacement */

    while (fgets (buf, MAXCH,fp)) {         /* read each line in file */
        p = buf;                            /* pointer to buf */
        while (*p) {
            if (*p == find[ndx])            /* if matches char in find */
                ndx++;                      /* advance index */
            else {  /* otherwise */
                if (ndx) {                  /* if find chars matched */
                    if (ndx == lfind)           /* if full word found */
                        fputs (repl, stdout);   /* output replacement */
                    else {  /* otherwise */
                        int tmp = repl[ndx];    /* save char at ndx */
                        repl[ndx] = 0;          /* nul-terminate repl */
                        fputs (repl, stdout);   /* output replacement */
                        repl[ndx] = tmp;        /* restore char at ndx */
                    }
                    ndx = 0;                /* zero index */
                }
                putchar (*p);       /* output current char */
            }
            p++;
        }
    }
    fclose (fp);                /* close file */

    if (ndx) {                  /* if partial match at end of file */
        repl[ndx] = 0;          /* nul-terminate repl at index */
        fputs (repl, stdout);   /* output final chars */
    }

(您应该进一步检查每一行的strlen是否合适并且没有被截断,否则您要查找的单词的不同部分可能会出现在两个不同的缓冲区中--这由您决定)

还要注意在循环退出后的检查,如果ndx为非零,则检查是否输出任何最后的字符。

在一个简短的示例中,以文件名读取和find/repl字符串作为程序的参数1、2、3(如果没有作为参数提供,则将提示find/repl ),您可以执行类似以下操作:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXCH 1024
#define MAXWD  128

void handle_args (int argc, char **argv, char *find, char *repl);

int main (int argc, char **argv) {

    size_t lfind, ndx = 0;
    char buf[MAXCH], find[MAXWD] = "", repl[MAXWD] = "", *p;
    FILE *fp = NULL;

    if (argc < 2 ) {    /* validate at least one argument given */
        fprintf (stderr, "error: insufficient input, usage: "
                        "%s filename [find, repl]\n", argv[0]);
        return 1;
    }
    if (!(fp = fopen (argv[1], "r"))) { /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    handle_args (argc, argv, find, repl);   /* set/prompt for find/repl */
    lfind = strlen (find);                  /* length of replacement */

    while (fgets (buf, MAXCH,fp)) {         /* read each line in file */
        p = buf;                            /* pointer to buf */
        while (*p) {
            if (*p == find[ndx])            /* if matches char in find */
                ndx++;                      /* advance index */
            else {  /* otherwise */
                if (ndx) {                  /* if find chars matched */
                    if (ndx == lfind)           /* if full word found */
                        fputs (repl, stdout);   /* output replacement */
                    else {  /* otherwise */
                        int tmp = repl[ndx];    /* save char at ndx */
                        repl[ndx] = 0;          /* nul-terminate repl */
                        fputs (repl, stdout);   /* output replacement */
                        repl[ndx] = tmp;        /* restore char at ndx */
                    }
                    ndx = 0;                /* zero index */
                }
                putchar (*p);       /* output current char */
            }
            p++;
        }
    }
    fclose (fp);                /* close file */

    if (ndx) {                  /* if partial match at end of file */
        repl[ndx] = 0;          /* nul-terminate repl at index */
        fputs (repl, stdout);   /* output final chars */
    }

    return 0;
}

/* simple function to set find/repl from command line, or 
 * prompt for input if no arguments given.
 */
void handle_args (int argc, char **argv, char *find, char *repl)
{
    if (argc < 3) {
        fputs ("enter find word: ", stdout);
        if (scanf ("%127s", find) != 1) {
            fputs ("error: invalid input.\n", stderr);
            exit (EXIT_FAILURE);
        }
    }
    else {
        size_t len = strlen (argv[2]);
        if (len < MAXWD)
            memcpy (find, argv[2], len + 1);
        else {
            fputs ("error: find argument too long.\n", stderr);
            exit (EXIT_FAILURE);
        }
    }

    if (argc < 4) {
        fputs ("enter repl word: ", stdout);
        if (scanf ("%127s", repl) != 1) {
            fputs ("error: invalid input.\n", stderr);
            exit (EXIT_FAILURE);
        }
    }
    else {
        size_t len = strlen (argv[3]);
        if (len < MAXWD)
            memcpy (repl, argv[3], len + 1);
        else {
            fputs ("error: repl argument too long.\n", stderr);
            exit (EXIT_FAILURE);
        }
    }
}

输入文件示例

代码语言:javascript
复制
$ cat ../dat/qbfox3.txt
A quick brown fox jumps over the lazy dog.
A slow green dog jumps on top of the blue cat.
A quick blue bird flew over the lazy dog too.

示例使用/输出

代码语言:javascript
复制
$ ./bin/file_replace_fgets_stdout ../dat/qbfox3.txt dog duck
A quick brown fox jumps over the lazy duck.
A slow green duck jumps on top of the blue cat.
A quick blue bird flew over the lazy duck too.

或检查第一个和最后一个字符替换的边角大小写,例如

代码语言:javascript
复制
$ ./bin/file_replace_fgets_stdout ../dat/qbfox3.txt "." "*"
A quick brown fox jumps over the lazy dog*
A slow green dog jumps on top of the blue cat*
A quick blue bird flew over the lazy dog too*

$ ./bin/file_replace_fgets_stdout ../dat/qbfox3.txt A B
B quick brown fox jumps over the lazy dog.
B slow green dog jumps on top of the blue cat.
B quick blue bird flew over the lazy dog too.

仔细检查一下,如果你还有其他问题,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54281647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档