0

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



}
rozak
  • 41
  • 1
  • 7

1 Answers1

1

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.

awerchniak
  • 357
  • 3
  • 16
  • Okay so now I've added this and it manages to write the very last sentence of the second file and nothing more.. making progress though! here's what I have `out = fopen(output, "w"); fprintf(out, ch); fclose(out); ` I'm guessing I need to add a pointer somewhere? – rozak Dec 12 '16 at 19:36
  • @rozak Don't keep opening and closing the file. Open it once at the beginning, close it at the end, and do all the writes in the middle. – Barmar Dec 12 '16 at 19:52
  • @Barmar so where exactly should I be putting the `fprintf`? just before I close the file streams fp1 and fp2? I don't really understand what you mean by "the middle" – rozak Dec 12 '16 at 20:25
  • The `fprintf` calls should be in your `while` loop. `open` should be before the loop, and `close` should be after the loop. – Barmar Dec 12 '16 at 20:29
  • I'm a bit confused as to what order you want to write these to the file. Think of it this way- stdout is just a special 'file,' at least in Unix-based operating systems. So, if you're trying to print to the file in the same order you're printing to the terminal, open a file pointer for writing, change those 'stdout's in your fputs() call to the name of that pointer, and then close it at the end – awerchniak Dec 12 '16 at 20:29
  • so I've done this: ` while (!feof(fp1)) { fgets(ch, 200, fp1); fputs(ch, stdout); out = fopen(output, "w"); fprintf(out, ch); }` and it's reading parts of the file but still not properly.. any idea? @Barmar @AndyW – rozak Dec 12 '16 at 20:50
  • As @Barmar said, don't keep reopening the file within the loop. Open it once at the beginning, before the loop, and then close it once at the end, after the loop. – awerchniak Dec 12 '16 at 20:52
  • See [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) `while (!feof(fp1)) { fgets(ch, 200, fp1);...` will copy the last line from `fp1` **twice** since `feof()` doesn't return non-zero until *after* the `EOF` is reached, so the last line is read then written to the output file, `feof()` returns zero, `fgets()` then fails *and leaves the last line read in the buffer* where it gets written to the output file again. – Andrew Henle Dec 12 '16 at 21:28