实施“猫”;在c中-wwwmc? (我的代码有什么问题)

发布于 2024-12-13 04:38:33 字数 847 浏览 6 评论 0原文

我的代码在某种程度上运行良好。不过我有两个问题。一,在将文件打印到标准输出结束时,它给了我一个分段错误。

第二,我无法使用 fputs 打印数据,因为我立即遇到分段错误。因此,为了修复它,我使用了 put ,它可以很好地打印它,但在每行后面添加一个 '\n' ,使文本单行间隔以及末尾的段错误。

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

void concat(char *arg){

    char string[256];
    FILE *fp = fopen(arg, "r");

    while(!feof(fp)){
        fgets(string, 256, fp);
        //fputs(string, fp);
        puts(string);
    }

    fclose(fp);

}

void stdincat(){

    char string[256];   
    while(!feof(stdin)){
        fgets(string, 256, stdin);
        fputs(string, stdout);
    }
}

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

    char argvalues[256][40];

    if(argv[1] == NULL)
        stdincat();
    else if(argv[1] != NULL){
        int i;

        for(i=1;i<=(argc);i++){
            concat(argv[i]);
        }
    }

    return 0;
}

My code works fine in a way. I have two issues with it though. One, at the end of printing the files to the standard output, it gives me a segmentation fault.

Two, I can't use fputs to print out the data because I get a segmentation fault right away. So to fix it I use puts which prints it fine, but adds a '\n' after every line making the text single line spaced as well as the seg fault at the end.

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

void concat(char *arg){

    char string[256];
    FILE *fp = fopen(arg, "r");

    while(!feof(fp)){
        fgets(string, 256, fp);
        //fputs(string, fp);
        puts(string);
    }

    fclose(fp);

}

void stdincat(){

    char string[256];   
    while(!feof(stdin)){
        fgets(string, 256, stdin);
        fputs(string, stdout);
    }
}

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

    char argvalues[256][40];

    if(argv[1] == NULL)
        stdincat();
    else if(argv[1] != NULL){
        int i;

        for(i=1;i<=(argc);i++){
            concat(argv[i]);
        }
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮颜 2024-12-20 04:38:33

您在 concat 中注释掉的对 fputs 的调用正在尝试写入 fp,您打开它只是为了阅读,所以毫不奇怪它不会/不起作用。

除此之外,您的阅读循环: while(!feof(fp)) { (以及类似的,除了来自 stdin 而不是 fp)遵循熟悉的、广泛存在的反模式——像这样的循环不能/不会/不能正常工作。您通常希望在同一操作中阅读并测试是否成功:

while(fgets(string, 256, stdin))
    fputs(string, stdout);

编辑:我还应该提到,我希望避免在 concatstdincat 中重复代码。我宁愿传递 FILE * 作为参数进行读取,因此您可以使用相同的代码从 stdin 或其他任意文件读取:

// Warning: untested code.
// error checking omitted for readability.
void catfile(FILE *file) { 
    char line[256];
    while (fgets(line, sizeof(line), file))
        puts(line);
}

int main(int argc, char **argv) { 
    int i;
    if (argc == 1)
        catfile(stdin);
    else for (int i=1; i<argc; i++) {
        FILE *infile = fopen(argv[i], "r");
        catfile(infile);
        fclose(infile);
    }
    return 0;
}

最后,我需要注意的是,如果您只是要复制整个文件,fgets 可能不是完成这项工作的最有效方法。 fread 可能更合适。当您这样做时,您也可以使用二进制模式读取和写入。

The call to fputs you have commented out in concat is attempting to write to fp, which you opened only for reading, so it's no surprise that it won't/doesn't work.

Other than that, your reading loops: while(!feof(fp)) { (and similar except from stdin instead of fp) follow a familiar, widespread anti-pattern -- loops like this don't/won't/can't work correctly. You normally want to read and test for success in the same operation:

while(fgets(string, 256, stdin))
    fputs(string, stdout);

Edit: I should also mention that I would prefer to avoid the duplication of code in concat and stdincat. I'd rather pass the FILE * to read from as the parameter, so you'd use the same code to read from stdin or from other arbitrary files:

// Warning: untested code.
// error checking omitted for readability.
void catfile(FILE *file) { 
    char line[256];
    while (fgets(line, sizeof(line), file))
        puts(line);
}

int main(int argc, char **argv) { 
    int i;
    if (argc == 1)
        catfile(stdin);
    else for (int i=1; i<argc; i++) {
        FILE *infile = fopen(argv[i], "r");
        catfile(infile);
        fclose(infile);
    }
    return 0;
}

Finally, I'd note that if you're just going to copy an entire file, fgets probably is not the most efficient way to do the job. fread may be more suitable. While you're at it, you might as well use binary mode reading and writing as well.

往日情怀 2024-12-20 04:38:33

您不能在仅用于读取而打开的流上写入,您应该使用 stdout 作为函数 concat 中的 fput

You cannot write on the stream that you opened only for read, you should use stdout for the fputs in the function concat

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文