Ç - 更换的话 [英] C - Replacing words

查看:120
本文介绍了Ç - 更换的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的目标是要读取从标准输入重定向的文件中的文本,然后更换一定的argv传递字改为。

例如,如果我运行:

  $ ./a.exe线474; input.txt中

其中,input.txt的是检测线合一,到了最后我应打印测试取代的。
我不太肯定我的code是怎么了,有时我得到段错误,我也不能肯定我怎么会去打印newOut字符串,或者如果我甚至需要之一。

作为一个方面说明,如果我读使用与fgets,如果什么字符59启动礼,那么,它开始作为第0指数下一个读命令,呢重读。难道这不是不计为一个字符串来的strstr搜索?

任何帮助是AP preciated,谢谢

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,字符** argv的){    焦炭FILEREAD [60];
    CHAR newOut [];
    而(!的feof(标准输入)){
        与fgets(FILEREAD,60,标准输入); //一次读取文件的60个字符
        如果(的strstr(FILEREAD,argv的[1])){如果argumentv [1]包含在FILEREAD //
            函数strncpy(newOut,FILEREAD,strlen的(ARGV [1])); //替换
        }
    }        返回(0);
}


解决方案

正如我在评论你的previous问题的 c - 要更换更好的方法:


  

这是显而易见的建议是阅读整行以与fgets()然后搜索这些(也许与的strstr())找到这个词来代替,然后再恢复从匹配单词后面的搜索结果在该行(这样的 [给测试前的字和替换文本之前打印材料的argv [1] ] 的含行测试,1,2,3,测试! 最终为更换!ING,1,2,3,更换!ED!


这是一个相当直观的实现所描述的算法。

 的#include<&ASSERT.H GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,字符** argv的)
{
    断言(argc个大于1);
    焦炭FILEREAD [4096]; / *给我一台台式电脑,其中这会导致麻烦! * /
    字符替换[] =所取代!;
    为size_t word_len = strlen的(的argv [1]);    而(与fgets(FILEREAD,sizeof的(FILEREAD),标准输入)!= 0)
    {
        字符*开始= FILEREAD;
        字符* word_at;
        而((word_at =的strstr(开始,argv的[1]))!= 0)
        {
            的printf(%* s%S,(INT)(word_at - 启动),启动,更换);
            开始= word_at + word_len;
        }
        的printf(%S,启动);
    }    返回(0);
}

请注意,断言()的位置使这个C99 code;将它放在 word_len 的定义之后,它变成C89 code。

My goal here is to read text from a file redirected from stdin, then replace certain argv passed words with the word "Replaced".

For example, if I run:

$ ./a.exe line < input.txt

where input.txt is "Test line one", at the end I should print "Test Replaced one." I'm not quite sure where my code is going wrong, sometimes I get segmentation fault, and I'm also not sure how I would go about printing the newOut string, or if I even need one.

As a side note, if I was reading using fgets, what if the 59th character started "li" then as it started reading again as the 0th index for the next read command, "ne". Wouldn't that not count as one string for strstr to search?

Any help is appreciated, thanks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

    char fileRead[60];  
    char newOut[];
    while (!feof(stdin)){ 
        fgets(fileRead,60,stdin); //read file 60 characters at a time
        if (strstr(fileRead,argv[1])){ // if argumentv[1] is contained in fileRead
            strncpy(newOut, fileRead, strlen(argv[1])); // replace 
        }        
    }

        return (0);
}

解决方案

As I observed in the comments to your previous question, C — A better method for replacing:

An obvious suggestion is to read whole lines with fgets() and then search those (maybe with strstr()) to find the word to be replaced, and then print the material before the word and the replacement text before resuming the search from after the matched word in the line (so [given "test" as argv[1]] a line containing "testing, 1, 2, 3, tested!" ends up as "Replaced!ing, 1, 2, 3, Replaced!ed!".

This is a rather straight-forward implementation of the described algorithm.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    assert(argc > 1);
    char fileRead[4096];  /* Show me a desktop computer where this causes trouble! */
    char replace[] = "Replaced!"; 
    size_t word_len = strlen(argv[1]);

    while (fgets(fileRead, sizeof(fileRead), stdin) != 0)
    {
        char *start = fileRead;
        char *word_at;
        while ((word_at = strstr(start, argv[1])) != 0)
        {
            printf("%.*s%s", (int)(word_at - start), start, replace);
            start = word_at + word_len;
        }
        printf("%s", start);
    }

    return (0);
}

Note that the position of the assert() makes this C99 code; place it after the definition of word_len and it becomes C89 code.

这篇关于Ç - 更换的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆