C - 读取换行符的末尾

发布于 2025-01-07 07:57:15 字数 1810 浏览 2 评论 0原文

我正在尝试编写一小段代码,该代码交替合并两个文件中的行并将结果写入另一个文件,所有这些都由用户指定。

不过,目前它似乎忽略了“\0”字符并一次复制整个文件,而不是一次复制一行。

#include <stdio.h>
#include <stdbool.h>

int main (void)
{

    char in1Name[64], in2Name[64], outName[64];
    FILE *in1, *in2, *out;

    printf("Enter the name of the first file to be copied: ");
    scanf("%63s", in1Name);

    printf("Enter the name of the second file to be copied: ");
    scanf("%63s", in2Name);

    printf("Enter the name of the output file: ");
    scanf("%63s", outName);


    // Open all files for reading or writing
    if ( (in1 = fopen(in1Name, "r")) == NULL )  
    {
        printf("Error reading %s", in1Name);
        return 1;
    }

    if ( (in2 = fopen(in2Name, "r")) == NULL )
    {
        printf("Error reading %s", in2Name);
        return 2;
    }

    if ( (out = fopen(outName, "w")) == NULL )
    {
        printf("Error writing to %s", outName);
        return 3;
    }


    // Copy alternative lines to outFile
    bool notFinished1 = true, notFinished2 = true;
    int c1, c2;
    while ( (notFinished1) || (notFinished2) ) 
    {

        while ( ((c1 = getc(in1)) != '\0') && (notFinished1) )
        {
            if (c1 == EOF)
            {
                notFinished1 = false;
            }
            else
            {
                putc(c1, out);
            }
        }

        while ( ((c2 = getc(in2)) != '\0') && (notFinished2) )
        {
            if (c2 == EOF)
            {
                notFinished2 = false;
            }
            else
            {
                putc(c2, out);
            }
        }

    } 


    // Close files and finish
    fclose(in1);
    fclose(in2);
    fclose(out);

    printf("Successfully copied to %s.\n", outName);

    return 0;

}

I'm trying to write a small piece of code that merges lines alternatively from two files and writes the results to another file, all specified by the user.

Except, at the moment it seems to be ignoring the '\0' character and copies the entire file at a time, instead of a line at a time.

#include <stdio.h>
#include <stdbool.h>

int main (void)
{

    char in1Name[64], in2Name[64], outName[64];
    FILE *in1, *in2, *out;

    printf("Enter the name of the first file to be copied: ");
    scanf("%63s", in1Name);

    printf("Enter the name of the second file to be copied: ");
    scanf("%63s", in2Name);

    printf("Enter the name of the output file: ");
    scanf("%63s", outName);


    // Open all files for reading or writing
    if ( (in1 = fopen(in1Name, "r")) == NULL )  
    {
        printf("Error reading %s", in1Name);
        return 1;
    }

    if ( (in2 = fopen(in2Name, "r")) == NULL )
    {
        printf("Error reading %s", in2Name);
        return 2;
    }

    if ( (out = fopen(outName, "w")) == NULL )
    {
        printf("Error writing to %s", outName);
        return 3;
    }


    // Copy alternative lines to outFile
    bool notFinished1 = true, notFinished2 = true;
    int c1, c2;
    while ( (notFinished1) || (notFinished2) ) 
    {

        while ( ((c1 = getc(in1)) != '\0') && (notFinished1) )
        {
            if (c1 == EOF)
            {
                notFinished1 = false;
            }
            else
            {
                putc(c1, out);
            }
        }

        while ( ((c2 = getc(in2)) != '\0') && (notFinished2) )
        {
            if (c2 == EOF)
            {
                notFinished2 = false;
            }
            else
            {
                putc(c2, out);
            }
        }

    } 


    // Close files and finish
    fclose(in1);
    fclose(in2);
    fclose(out);

    printf("Successfully copied to %s.\n", outName);

    return 0;

}

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

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

发布评论

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

评论(3

若相惜即相离 2025-01-14 07:57:15

换行符是'\n',而不是'\0'。后者是一个零值(null)字节;在 C 内部,它用于指示字符串的结尾,但文本文件不包含它。

The newline character is '\n', not '\0'. The latter is a zero-valued (null) byte; inside C, it's used to indicate the end of a string, but text-files don't contain it.

对风讲故事 2025-01-14 07:57:15

如果这些是文本文件,则每行后面通常不会有 \0 —— 这几乎专门用于内存中的字符串。 \n 是换行符,很可能是您要检查的字符。

If these are text files, there won't normally be a \0 after each line -- that's almost exclusively for in-memory strings. \n is the newline char, and more than likely the char you want to check for.

人心善变 2025-01-14 07:57:15

我浏览了你的代码,发现了错误。要逐行复制文件,您应该查找“\n”而不是“\0”。 '\0' 仅终止字符串,并不指定新行。将“\0”的两个实例替换为“\n”将解决您的问题。

I have gone through your code, and found the error. To copy the files line by line you should be looking for '\n' instead of '\0'. The '\0' only terminates the string, it doesn't designate a new line. Replacing both instances of '\0' with '\n' will fix your problem.

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