Home > Back-end >  Copy words with given letter from file to new file
Copy words with given letter from file to new file

Time:06-06

I'm trying to copy words from one file to another, but the words must begin with the given letter. It's working but doesn't copy every word that matches.

#include <stdio.h>

int main() {
    FILE *f = fopen("words.txt", "r");
    FILE *f2 = fopen("words_copy.txt", "a ");
    char usr;
    printf("enter letter: ");
    scanf("%c", &usr);
    char buffer[255];
    char ch, ch2;
    while ((ch = fgetc(f)) != EOF) {
        ch2 = fgetc(f);
        if (ch2 == usr && ch == '\n') {
            fputc(ch2, f2);
            fgets(buffer, sizeof(buffer), f);
            fputs(buffer, f2);
        }
    }

    return 0;
}

Words.txt contains:

adorable aesthetic  alluring angelic appealing arresting attractive
blooming charismatic charming cherubic chocolate-box classy contagious
cute dazzling debonair decorative delectable delicate distinguished
enchanting enticing eye-catching glamorous glossy good-looking
gorgeous infectious lovely lush magnetic magnificent majestic melting
mesmerizing noble picturesque poetic prepossessing shimmering striking
stunning winsome

every word is in next line, when I'm running the program and giving the letter m words_copy.txt contains only:

magnificent melting

How to fix to copy every word with matching letter?

CodePudding user response:

The test in the loop is incorrect: you check the first letter after a newline and output the line if there is a match. With this logic:

  • you cannot match the first word in the file
  • you only match words starting with usr
  • and the word following a match is ignored

Furthermore, you ch and ch2 should be defined with type int to match EOF reliably, you should test for fopen failure and close the files after use.

You should use a simpler approach:

  • read a word
  • test if it contains the letter
  • output the word if it matches

Here is a modified version:

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

int main() {
    char usr;
    char buffer[256];
    int ch = 0;
    size_t pos;

    FILE *f = fopen("words.txt", "r");
    if (f == NULL) {
        fprintf(stderr, "cannot open words.txt: %s\n", strerror(errno));
        return 1;
    }
    FILE *f2 = fopen("words_copy.txt", "a ");
    if (f2 == NULL) {
        fprintf(stderr, "cannot open words_copy.txt: %s\n", strerror(errno));
        fclose(f);
        return 1;
    }

    printf("enter letter: ");
    if (scanf(" %c", &usr) != 1) {
        fprintf(stderr, "missing input\n");
        fclose(f);
        fclose(f2);
        return 1;
    }
    while (ch != EOF) {
        pos = 0;
        /* read a word, stop at whitespace and end of file */
        while ((ch = fgetc(f)) != EOF && !isspace(ch)) {
            if (pos   1 < sizeof(buffer))
                buffer[pos  ] = (char)ch;
        }
        buffer[pos] = '\0';
        /* test for a match */
        if (strchr(buffer, usr)) {
            /* output matching word */
            fprintf(f2, "%s\n", buffer);
        }
    }
    fclose(f);
    fclose(f2);
    return 0;
}
  • Related