c - 从两个文件读取输入并将其写入 C 中的另一个文件

标签 c file input stream output

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

#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);



}

最佳答案

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

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

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

关于c - 从两个文件读取输入并将其写入 C 中的另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41107404/

相关文章:

c - 词法分析器输出问题

c - 动态二维数组 - 分配不起作用(段错误)

C++:读写类

iphone - 给定文件和目录生成相对路径的 Objective-C 代码

javascript - 如何从输入数组中获取多维数组

javascript - 在 javascript 中更改另一个输入时更改输入

jquery - 当两个输入都被填充时更改图像

c - 从 void 转换为 int* 后无法访问指针的地址

无法访问结构体字段

java - 比较方法在排序文件时违反了它的一般契约