逐行添加

时间:2016-12-12 20:14:53

标签: c

我目前必须添加,并按照这样的顺序进行操作

content of file 1
content of file 2
content of file 1
content of file 2

它目前只将一个文本文件的内容写入输出文件,我无法看到我哪里出错了所以非常感谢任何帮助。

****编辑,嗨做了一些挖掘,发现这个确切的问题已经得到了解答,并不想与mods有问题而且无法删除,谢谢所有

3 个答案:

答案 0 :(得分:1)

您没有存储fopen()返回的流指针,并且您没有交错来自file1和file2的行。以下是解决这些问题的方法:

    ...
    // file 3
    printf("Please enter the name of the output file : ");
    if (scanf("%s", file3) != 1) {
        printf("input error\n");
        exit(1);
    }    
    FILE *OpenFile = fopen(file1, "r");
    FILE *OpenFile2 = fopen(file2, "r");
    FILE *OpenFile3 = fopen(file3, "w");

    if (OpenFile == NULL || OpenFile2 == NULL || OpenFile3 == NULL) {
        perror("Error opening files");
        printf("Press any key to exit!\n");
        exit(1);
    }

    int c1 = 0, c2 = 0;
    while (c1 != EOF || c2 != EOF) {
        if (c1 != EOF) {
            /* copy a line from file1 */
            while ((c1 = fgetc(OpenFile)) != EOF && c1 != '\n') {
                fputc(c1, OpenFile3);
            }
            fputc('\n', OpenFile3);
        }
        if (c2 != EOF) {
            /* copy a line from file2 */
            while ((c2 = fgetc(OpenFile)) != EOF && c2 != '\n') {
                fputc(c2, OpenFile3);
            }
            fputc('\n', OpenFile3);
        }
    }

    printf("The two files were successfully merged into %s\n", file3);

    fclose(OpenFile);
    fclose(OpenFile2);
    fclose(OpenFile3);
    return 0;

答案 1 :(得分:0)

您需要轮换fgetc次来电,而不是一次性完成每个循环。

int file2_ok = 1, file_ok = 1;
while (file_ok && file2_ok) {
    if (file2_ok) {
        c = fgetc(OpenFile2);
        if (c == EOF) {
            file2_ok = 0;
        } else {
            fputc(c, OpenFile3);
        }
    }
    if (file_ok) {
        c = fgetc(OpenFile);
        if (c == EOF) {
            file_ok = 0;
        } else {
            fputc(c, OpenFile3);
        }
    }
}

这假定这两个文件具有相同的字符数。如果这些问题不能均匀匹配,那么这个问题并不能说明它们应该如何合并。

答案 2 :(得分:0)

此合并交替显示两个文本文件的行。输入文件和输出文件是程序参数。

#define _CRT_SECURE_NO_WARNINGS

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

int main(int argc, char *argv[]){
    FILE *OpenFile;
    FILE *OpenFile2;
    FILE *OpenFile3;
    char buffer[10000];                     // hopeful
    int finished = 0;

    if(argc < 4) {
        exit(1);
    }
    OpenFile  = fopen(argv[1], "rt");       // "t" is for MSVC
    OpenFile2 = fopen(argv[2], "rt");
    OpenFile3 = fopen(argv[3], "wt");
    if(OpenFile == NULL || OpenFile2 == NULL || OpenFile3 == NULL) {
        exit(1);
    }

    while(finished != 3) {
        if(fgets(buffer, sizeof buffer, OpenFile) != NULL) {
            fputs(buffer, OpenFile3);
        }
        else {
            finished |= 1;
        }

        if(fgets(buffer, sizeof buffer, OpenFile2) != NULL) {
            fputs(buffer, OpenFile3);
        }
        else {
            finished |= 2;
        }
    }

    fclose(OpenFile);
    fclose(OpenFile2);
    fclose(OpenFile3);
    return 0;
}

输入文件1:

File 1 line 1
File 1 line 2
File 1 line 3
File 1 line 4

输入文件2:

File 2 line 1
File 2 line 2

输出文件3:

File 1 line 1
File 2 line 1
File 1 line 2
File 2 line 2
File 1 line 3
File 1 line 4

如果注意到finished状态而不是在任何文件到达fgets后调用EOF,则解决方案可能会更有效。