复制带有文件的字母到新文件的单词

发布于 2025-02-04 21:50:17 字数 1216 浏览 2 评论 0原文

我正在尝试将单词从一个文件复制到另一个文件,但是单词必须从给定的字母开始。它正在工作,但不复制每个与匹配的单词。

#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 包含:

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

每个单词都在下一行, 当我运行程序并给出字母m words_copy.txt 仅包含:

magnificent melting

如何修复以匹配字母复制每个单词?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮倦 2025-02-11 21:50:17

循环中的测试不正确:您在新线后检查第一个字母,并在有匹配的情况下输出线路。使用此逻辑:

  • 您无法匹配文件中的第一个单词
  • ,您只匹配usr
  • ,并且匹配后的单词被忽略

,您ch and code> > CH2应使用类型int来匹配eof可靠地测试fopen失败并在使用后关闭文件。

您应该使用一种更简单的方法:

  • 情况下,请阅读一个单词
  • 如果包含字母
  • 输出单词,则在此匹配的

测试:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文