我刚刚在c中写了一个简单的代码,它应该从文件中提取一个文本,并用另一个单词替换一个单词。然而,我不知道为什么,但我的代码只是替换从第二个字母开始的单词。我做错了什么?这是我的代码:
#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);
}
}
我觉得你的方法有点太复杂了,只需移动指针就可以简单得多。这是一个粗略的草图:
#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 ` ` */
/* 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 %sn", 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);
}
继续我的评论,永远不要使用gets()
,它非常不安全,并且容易被缓冲区溢出所利用。相反,对于面向行的输入,请使用fgets
或POSIXgetline
。
当读取文本文件作为输入时,99%的时间都正确地看待面向行的输入函数。它在这里工作得很好,但你必须确保你的线路缓冲区足以容纳你预期的最大线路。
您也不应该试图修改您正在"就地"搜索的文件。如果要查找和替换的单词的长度完全相同,则可能会发生这种情况,但您必须小心,否则会损坏文件。在写入新文件时,您可以自由使用长度不同的"查找"one_answers"替换"单词。
假设你的单词将完全包含在你阅读的行中(而不是连字符或以其他方式拆分为多行),你可以简单地阅读每一行,将指针分配给行中的起始字符,然后将指针沿着行向下移动,当字符与查找单词中的字符匹配时保持索引,并在字符不同时输出替换项。您必须考虑如何处理重置索引和处理部分匹配,但这只是一个算术问题。
例如,如果您有一个指向打开的文件流(例如fp
)的指针,将每一行读取到buf
中,并在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 */
}
(你应该进一步检查每一行的strlen
,它是否适合并且没有被截断,否则你要寻找的单词的不同部分可能会在两个不同的缓冲区中——这就留给你了)
还要注意循环退出后的检查,如果ndx
为非零,则会检查是否输出任何最终字符。
放在一个简短的例子中,将读取和查找/rep字符串的文件名作为程序的参数1、2、3(如果没有作为参数提供,则会提示查找/rep),您可以执行以下操作:
#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);
}
}
}
示例输入文件
$ 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.
示例使用/输出
$ ./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.
或者检查第一个和最后一个字符替换的角点情况,例如
$ ./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.
仔细看看,如果你还有问题,请告诉我。