使用c的库编辑txt文件



我在扫描现有文件时遇到问题。

挑战在于我有一个带有一些字符串的源文本文件。我必须扫描这个文件寻找"o'clock"这个词,在我找到它的地方,我必须把"o'clock"前面的单词用方括号括起来。

我找到了一个函数来找到触发器,但我不明白接下来我需要做什么。如果您能解释一下如何替换读字符串中的字符,我将不胜感激。

例如:我们有一个字符串:

"Let's go to the graveyard at night - at twelve o'clock!"

我需要将单词twelve替换为[twelve]

代码如下:

#include <stdio.h>
#include <string.h>
int main() {
FILE * fp; //open exists file
FILE * fn; //open empty file
char c[1000]; // buffer
char trigger[] = "o'clock"; // the word before which the characters should be added
char name [] = "startFile.txt"; // name of source file
char secondName[] = "newFile.txt"; // name of output file
fp = fopen (name, "r"); // open only for reading
if (fp == NULL) { // testing on exists
printf ( "Error");
getchar ();
return 0;
}
fn = fopen(secondName, "w"); // open empty file for writing
while (!feof(fp)) { // scanning all string in the source
if(fgets(c,1000, fp) != NULL) { // reading charter from file
fprintf(fn, c); // writing charter from buffer "c" to empty file
if (strstr(c, trigger)) { // find triggered word
// I tried compare string which has a trigger-word
}
}
}
fclose(fp); // closing file
fclose(fn);
return 0;
}

在代码中,将该行复制到输出文件中,然后查找触发词。但我们的目标是找到触发器并编辑字符串,然后才将该行写入输出文件。此外,为了编辑该行,您需要知道在哪里找到了触发器,因此需要保存搜索的输出。

所以,而不是:

fprintf(fn, c); // writing charter from buffer "c" to empty file
if (strstr(c, trigger)) { // find triggered word
// I tried compare string which has a trigger-word
}

我们需要:

char *p = strstr(c, trigger);
if (p) {
// I tried compare string which has a trigger-word
}
fputs(c, fn); // writing charter from buffer "c" to empty file

现在我们找到了触发器,我们需要向后搜索以找到结尾,然后找到前面单词的开头。当我们找到它们时,我们需要创建一个位置并插入编辑:

char *p = strstr(c, trigger);
if (p) {
// ok we found the trigger - now we need to search backwards
// to find the end and then the start of the word before
while (p>c && p[-1]==' ') p--; // find the end of the word
memmove(p+1, p, c+999 - p); // make a space for the ]
*p = ']';
while (p>c && p[-1]!=' ') p--; // find the start of the word
memmove(p+1, p, c+999 - p); // make a space for the [
*p = '[';
}
fputs(c, fn); // writing charter from buffer "c" to empty file

试试:https://www.onlinegdb.com/H18Tgy9xu

但是,如果有不止一个"o-clock"呢?在绳子上?像这样:

我们12点去墓地,提前6点吃晚饭吧!

在这种情况下,您需要循环,直到找到所有的

下面是最后的代码:
#include <stdio.h>
#include <string.h>
int main() {
FILE * fp; //open exists file
FILE * fn; //open empty file
char c[1000]; // buffer
char trigger[] = "o'clock"; // the word before which the characters should be added
char name [] = "startFile.txt"; // name of source file
char secondName[] = "newFile.txt"; // name of output file
fp = fopen (name, "r"); // open only for reading
if (fp == NULL) { // testing on exists
printf ( "Error");
getchar ();
return 0;
}
fn = fopen(secondName, "w"); // open empty file for writing
if (fn == NULL) { // testing on create
printf ( "Error");
getchar ();
return 0;
}
while (fgets(c, 1000, fp)) { // scanning all string in the source
char *p = c; // we need to start searching at the beginning
do {
p = strstr(p+1, trigger); // find the next oclock after this one
if (p) {
// ok we found the trigger - now we need to search backwards
// to find the end and then the start of the word before
while (p>c && p[-1]==' ') p--; // find the end of the word
memmove(p+1, p, c+999 - p); // make a space for the ]
*p = ']';
while (p>c && p[-1]!=' ') p--; // find the start of the word
memmove(p+1, p, c+999 - p); // make a space for the [
*p = '[';
p = strstr(p, trigger); // find that same oclock again
// (we couldn't save the location because memmove has changed where it is)
}
} while (p); // as long as we found one, search again
fputs(c, fn); // writing charter from buffer "c" to empty file
}
fclose(fp); // closing file
fclose(fn);
return 0;
}

试试:https://onlinegdb.com/SJMrP15ld

最新更新