简体   繁体   中英

Read input from two files and write them into another file in C

I'm trying to make a program that reads in two files and writes the content from both into a separate file. My program already reads both files and displays their statistics and I just need to get it to write it into the new output file. I think I'm almost there I just need a little help to finish it off. Here is my code: (the output file stream is at the end of the code)

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



}

If you can print it to the terminal, then you can print it to a file! printf("") is merely a special way of saying fprintf(stdout, "") . So, open a new file pointer to the file you want to print out to, and set that as the target of an fprintf() call. For example,

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

EDIT: I see you were using fputs to print out. This is the same principle as what I mentioned above. Rather than piping to stdout, open a file for writing and pipe there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM