使用C中的库编辑txt文件

使用C中的库编辑txt文件,c,cstdio,C,Cstdio,我在扫描现有文件时遇到问题 挑战在于我有一个带有一些字符串的源文本文件。我必须扫描此文件以查找单词“o'clock”,在找到它的地方,我必须将单词置于“o'clock”之前,并将其括在方括号中 我找到了一个查找触发器的函数,但我不知道接下来需要做什么。如果您能解释如何替换读取字符串中的字符,我将不胜感激 例如:我们有一个字符串: “我们晚上十二点去墓地吧!” 我需要将单词seven替换为[seven] 代码如下: #include <stdio.h> #include <str

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

挑战在于我有一个带有一些字符串的源文本文件。我必须扫描此文件以查找单词
“o'clock”
,在找到它的地方,我必须将单词置于
“o'clock”
之前,并将其括在方括号中

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

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

“我们晚上十二点去墓地吧!”

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

代码如下:

#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;
}
#包括
#包括
int main(){
FILE*fp;//打开存在的文件
FILE*fn;//打开空文件
字符c[1000];//缓冲区
char trigger[]=“o'clock”;//应在其前面添加字符的单词
char name[]=“startFile.txt”;//源文件的名称
char secondName[]=“newFile.txt”;//输出文件的名称
fp=fopen(名称,“r”);//仅为阅读而打开
如果(fp==NULL){//测试存在
printf(“错误”);
getchar();
返回0;
}
fn=fopen(secondName,“w”);//打开空文件进行写入
当(!feof(fp)){//扫描源中的所有字符串时
if(fgets(c,1000,fp)!=NULL){//从文件中读取章程
fprintf(fn,c);//将charter从缓冲区“c”写入空文件
if(strstr(c,trigger)){//查找触发字
//我尝试比较有触发字的字符串
}
}
}
fclose(fp);//关闭文件
fclose(fn);
返回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
请在此处尝试:

但是,如果字符串中有多个“o”怎么办?大概是这样的:

我们十二点钟去墓地,六点钟吃晚饭吧

在这种情况下,您需要循环,直到找到所有这些文件:

以下是最终代码:

#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;
}
#包括
#包括
int main(){
FILE*fp;//打开存在的文件
FILE*fn;//打开空文件
字符c[1000];//缓冲区
char trigger[]=“o'clock”;//应在其前面添加字符的单词
char name[]=“startFile.txt”;//源文件的名称
char secondName[]=“newFile.txt”;//输出文件的名称
fp=fopen(名称,“r”);//仅为阅读而打开
如果(fp==NULL){//测试存在
printf(“错误”);
getchar();
返回0;
}
fn=fopen(secondName,“w”);//打开空文件进行写入
如果(fn==NULL){//create上的测试
printf(“错误”);
getchar();
返回0;
}
while(fgets(c,1000,fp)){//扫描源中的所有字符串
char*p=c;//我们需要从头开始搜索
做{
p=strstrstr(p+1,trigger);//查找此oclock之后的下一个oclock
如果(p){
//好的,我们找到了触发器-现在我们需要向后搜索
//先找到单词的结尾,然后再找到单词的开头
while(p>c&&p[-1]='')p--;//查找单词的结尾
memmove(p+1,p,c+999-p);//为
*p=']';
while(p>c&&p[-1]!='')p-->//查找单词的开头
memmove(p+1,p,c+999-p);//为[
*p='[';
p=strstr(p,trigger);//再次找到相同的oclock
//(我们无法保存位置,因为memmove已更改位置)
}
}while(p);//只要我们找到一个,就再搜索一次
fputs(c,fn);//将特许从缓冲区“c”写入空文件
}
fclose(fp);//关闭文件
fclose(fn);
返回0;
}

这里试试:< /p>选择一种语言。C或C++(你的包含是C++,你的代码更像C)。也读你把它标记为C++,把它描述成C,然后使用<代码>文件> <代码>(c)和<代码>使用命名空间STD< /Cuff>(C++)这个代码有严重的身份危机。听起来像是笑话,但是我必须使用C++中的C++库:当使用C++时,使用C++。使用<代码> STD::String < /C> >代替<代码> char */COD>。使用“代替代码>文件*<代码>。在使用<代码>时,请打开编译器警告,指出使用<代码> char *<代码>之类的东西。const char*。听起来你好像陷入了一个完全无用的“C++”中只教你坏习惯的课程,以及编程是一种无尽的痛苦,因为它们错过了几乎所有东西的要点。希望你能在没有太多伤害的情况下生存下来!为了让生活变得简单,你可以将整个godbolt代码作为一个可下载/可运行的代码块发布。评论足够好了。@CraigEstey好主意,一个事实上,我总是这样做——我不知道为什么这次我没有这么做。:(我[有时]所做的与你所做的相似。一些解释性文本中穿插着一些小代码块。在底部,我发布完整的代码。如果OP正在学习[与有特定问题的经验丰富的程序员相比],我通常会采用三阶段方法。(1)对OP的代码进行最小的修改以修复bug(2)增强功能(如muliple o'clock)(3)完全清理我从头开始编写的代码。很多OP都表示