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

时间:2015-04-18 12:34:34

标签: 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 file\n");
        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;
}

2 个答案:

答案 0 :(得分:1)

您可以检查char是否属于这样的单词

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)来测试它。

答案 1 :(得分:0)

在下面的代码中,我使用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 file\n");
        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] = '\0'; // 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;
}