【问题标题】:Read input from two files and write them into another file in C从两个文件中读取输入并将它们写入 C 中的另一个文件
【发布时间】:2017-04-27 16:56:08
【问题描述】:

我正在尝试制作一个程序来读取两个文件并将两个文件中的内容写入一个单独的文件。我的程序已经读取了这两个文件并显示了它们的统计信息,我只需要让它将其写入新的输出文件。我想我快到了,我只需要一点帮助来完成它。这是我的代码:(输出文件流在代码末尾)

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

int main() {

    // declaring variables
    FILE *fp1;
    FILE *fp2;
    FILE *out;
    int charcount = 0, wordcount = 0, linecount = 1, sentence = 0;
    int charcount2 = 0, wordcount2 = 0, linecount2 = 1, sentence2 = 0;
    int character;
    char first[50];
    char second[50];
    char output[50];
    char ch[200];

    // asking the user for the file names and scanning the names they enter as txt files
    printf(" Enter the first file name: ");
    scanf("%s", first);
    strcat(first, ".txt");

    printf(" Enter the second file name: ");
    scanf("%s", second);
    strcat(second, ".txt");

    printf(" Enter the output file name: ");
    scanf("%s", output);
    strcat(output, ".txt");

    // opening the file stream
    fp1 = fopen(first, "r");

    // if the file cannot be reached, display error
    if (fp1 == NULL) {
        printf("File cannot be opened: %s\n", first);
        return 0;
    }

    // reading and printing the file into the program
    printf("\n---FIRST FILE---\n");
    while (!feof(fp1)) {
        fgets(ch, 200, fp1);
        fputs(ch, stdout);
    }

    // counting the characters, words and lines until the program is finished
    fseek(fp1, 0, SEEK_SET); // sends the file pointer back to the start of the file
    while ((character = fgetc(fp1)) != EOF) {
        if (character == EOF)
            break;
        {
            charcount++;
        }
        if (character == ' ' || character == '.')
        {
            wordcount++;
        }
        if (character == '\n')
        {
            linecount++;
        }
        if (character == '.')
        {
            sentence++;
        }
    }

    // closing the stream
    fclose(fp1);

    // printing the number of characters, words and lines
    printf("\n Characters: %d \n Words: %d\n Lines: %d\n Sentences: %d\n\n\n", charcount, wordcount, linecount, sentence);

    //---------SECOND FILE----------//

    // opening the stream
    fp2 = fopen(second, "r");

    // reading and printing the file into the program
    printf("\n---SECOND FILE---\n");
    while (!feof(fp2)) {
        fgets(ch, 200, fp2);
        fputs(ch, stdout);
    }

    // counting the characters, words and lines until the program is finished
    fseek(fp2, 0, SEEK_SET); // sends the file pointer back to the start of the file
    while ((character = getc(fp2)) != EOF) {
        if (character == EOF)
            break;
        {
            charcount2++;
        }
        if (character == ' ' || character == '.')
        {
            wordcount2++;
        }
        if (character == '\n')
        {
            linecount2++;
        }
        if (character == '.')
        {
            sentence2++;
        }
    }

    // closing the stream
    fclose(fp2);

    // printing the number of characters, words and lines
    printf("\n Characters: %d \n Words: %d\n Lines: %d\n Sentences: %d\n\n\n", charcount2, wordcount2, linecount2, sentence2);

    out = fopen(output, "w");

    fgets(ch, 0, fp1);
    fgets(ch, 0, fp2);

    fclose(out);



}

【问题讨论】:

    标签: c file input stream output


    【解决方案1】:

    如果您可以将其打印到终端,那么您可以将其打印到文件! printf("")只是fprintf(stdout, "")的一种特殊说法。因此,打开一个指向要打印到的文件的新文件指针,并将其设置为 fprintf() 调用的目标。例如,

    fp3 = fopen(<output file name>, "w")
    fprintf(fp3,<string you want to write>)
    fclose(fp3)
    

    编辑:我看到您正在使用 fputs 打印出来。这和我上面提到的原理是一样的。与其通过管道传输到标准输出,不如打开一个文件进行写入并在那里传输。

    【讨论】:

    • 好的,现在我已经添加了这个,它设法写了第二个文件的最后一句,仅此而已..虽然取得了进展!这就是我所拥有的out = fopen(output, "w"); fprintf(out, ch); fclose(out); 我猜我需要在某处添加一个指针?
    • @rozak 不要一直打开和关闭文件。开头打开一次,结尾关闭,中间写完。
    • @Barmar 那么我应该把fprintf 放在哪里呢?就在我关闭文件流 fp1 和 fp2 之前?我不太明白你所说的“中间”是什么意思
    • fprintf 调用应该在您的while 循环中。 open 应该在循环之前,close 应该在循环之后。
    • 对于您要将这些写入文件的顺序我有点困惑。可以这样想——stdout 只是一个特殊的“文件”,至少在基于 Unix 的操作系统中是这样。因此,如果您尝试以与打印到终端相同的顺序打印到文件,请打开文件指针进行写入,将 fputs() 调用中的那些“stdout”更改为该指针的名称,然后最后关闭它
    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多