首页 > 解决方案 > 如何从文件中读取特定单词?

问题描述

我有一个文件,其中包含单词及其同义词,每个单词都位于单独的行中。我正在编写这段代码,它应该逐行读取文件,然后从作为同义词的第二个单词开始显示它。我在第一个循环中使用了变量 count 以便能够计算每个单词的同义词数量,因为同义词的数量各不相同。此外,我使用了条件 synonyms[i]==',' 因为每个同义词都用逗号分隔。我编写此类代码的目的是将它们放在二叉搜索树中,以便拥有完整的字典。该代码不包含任何错误,但它不起作用。我已经尝试过每个循环,但这也没有用。

来自文件的示例输入:

abruptly - dead, short, suddenly
acquittance - release
adder - common, vipera

样本预期输出:

dead short suddenly
acquittance realse 
common vipera

这是代码:

void LoadFile(FILE *fp){
    int count; 
    int i;
    char synonyms[50]; 
    char word[50];
    while(fgets(synonyms,50,fp)!=NULL){
        for (i=0;i<strlen(synonyms);i++)
    if (synonyms[i]==',' || synonyms[i]=='\n')
        count++;
    }
    while(fscanf(fp,"%s",word)==1){
    for(i=1;i<strlen(synonyms);i++){
        ( fscanf(fp,"%s",synonyms)==1);
            printf("%s",synonyms);
        }
      }
    }
    int main(){
    char fn[]="C:/Users/CLICK ONCE/Desktop/Semester 4/i2206/Project/Synonyms.txt";
    FILE *fp;
    fp=fopen(fn,"rt");
    if (fp==NULL){
        printf("Cannot open this file");
    }
        else{
           LoadFile(fp);
        }
     return 0;
    }

标签: cdata-structures

解决方案


这是我的解决方案。为了便于阅读,我将工作拆分为函数。实际的解析是在parse函数中完成的。该函数考虑了带连字符的复合词,例如 72。单词和他的同义词必须用连字符隔开,前面至少有一个空格。

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

// Trim leading and trailing space characters.
// Warning: string is modified
char* trim(char* s) {
    char* p = s;
    int l = strlen(p);

    while (isspace(p[l - 1])) p[--l] = 0;
    while (*p && isspace(*p)) ++p, --l;

    memmove(s, p, l + 1);
    return s;
} 

// Warning: string is modified
int parse(char* line)
{
    char* token;
    char* p;
    char* word;

    if (line == NULL) {
        printf("Missing input line\n");
        return 0;
    }

    // first find the word delimiter: an hyphen preceded by a space
    p = line;
    while (1) {
        p = strchr(p, '-');
        if (p == NULL) {
            printf("Missing hypen\n");
            return 0;
        }
        if ((p > line) && (p[-1] == ' ')) {
            // We found an hyphen preceded by a space
            *p = 0; // Replace by nul character (end of string)
            break;
        }
        p++; // Skip hyphen inside hypheneted word
    }
    word = trim(line);
    printf("%s ", word);

    // Next find synonyms delimited by a coma
    char delim[] = ", ";
    token = strtok(p + 1, delim);
    while (token != NULL) {
        printf("%s ", token);
        token = strtok(NULL, delim);
    }
    printf("\n");
    return 1;
}

int LoadFile(FILE* fp)
{
    if (fp == NULL) {
        printf("File not open\n");
        return 0;
    }
    int ret = 1;
    char str[1024];  // Longest allowed line
    while (fgets(str, sizeof(str), fp) != NULL) {
        str[strcspn(str, "\r\n")] = 0; // Remove ending \n
        ret &= parse(str);
    }
    return ret;
}

int main(int argc, char *argv[])
{
    FILE* fp;
    char* fn = "Synonyms.txt";
    fp = fopen(fn, "rt");
    if (fp == NULL) {
        perror(fn);
        return 1;
    }
    int ret = LoadFile(fp);
    fclose(fp);
    return ret;
}

推荐阅读