逐字读取文件,没有结尾字符

  • 本文关键字:结尾 字符 读取 文件 c
  • 更新时间 :
  • 英文 :


我有一个C语言的小程序,它可以逐字逐句地读取文件,我怎么能保证像"这个"这样的词不会被读成一个词?我希望它读作"这个"

int main(int argc, char *argv[]) {
    if(argc != 3)
    {
        printf("Usage: ./sw <word> <filename> n");
        exit(1);
    }
    char* word = argv[1];
    const char* filename = argv[2];
    FILE* file = fopen(filename, "r");
    if(file == NULL)
    {
        printf("Could not open filen");
        exit(1);
    }
    //Assuming one word can not have more than 250 chars
    char w[250], check_eof;
    do 
    {
        check_eof = fscanf(file, "%s", w);
        if(strcmp(word, w) == 0)
        {
            printf("W : %s n", w);
        }
    } while(check_eof != EOF);
    fclose(file);
    return 0;
}

你可以检查一个字符是否属于这样的单词

int c = fgetc(file);
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
    // c belongs to a word
    word[n++] = c;
} else {
    // end of word
    if (strncmp(word, w, n) == 0) {
        // word and w match!
    }
}

如果#include <ctype.h>,则可以改为调用isalpha(c)进行测试。

在下面的代码中,我使用 isalpha() 并将结果字符串复制到名为 res 的新缓冲区中。但是,此过程可以就地完成,但为了简单起见,我现在离开。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // for isalpha()
int main(int argc, char *argv[]) {
    char* word = "this";
    const char* filename = "test.txt";
    FILE* file = fopen(filename, "r");
    if(file == NULL)
    {
        printf("Could not open filen");
        exit(1);
    }
    //Assuming one word can not have more than 250 chars
    // ATTENTION, it is 249 chars, do NOT forget of the null terminator
    char w[250], res[250];
    int check_eof; // should be of type int, for EOF checking
    do
    {
        check_eof = fscanf(file, "%s", w);
        // what if 'word' appears as the last word
        // in the file? You should check for eof
        // right after fscanf()
        if(check_eof == EOF)
          break;
        int i = 0, j = 0;
        while (w[i]) // parse what we read
        {
          if (isalpha(w[i]))
            res[j++] = w[i]; // keep only the alphabetic chars
          i++;
        }
        res[j] = ''; // it should be a null terminated string
        if(strcmp(word, res) == 0) // compare to 'res' now
        {
            printf("W : %s n", res);
        }
    } while(1); // the terminating step is inside the body now
    fclose(file);
    return 0;
}

最新更新