1

我正在尝试逐字阅读,以下是我采用的逻辑。这可以很好地读取单词,除非它到达一行中的最后一个单词,它存储当前文件的最后一个单词和下一个新行的第一个单词。有人可以告诉我如何让它工作吗?

int c;
int i =0;
char line[1000]
do{
    c = fgetc(fp);

    if( c != ' '){
        printf("%c", c);
    line[i++] = c;

    }else if((c == '\n')){

//this is where It should do nothing

    }else{
    line[i] = '\0';
    printf("\\0 reached\n");//meaning end of one word has been reached
    strcpy(wordArr[counter++].word, line);//copy that word that's in line[xxx] to the struct's .word Char array
    i=0;//reset the line's counter

    }//if loop end



} while(c != EOF);//do-while end

fp是一个文件指针。

HI BABY TYPE MAYBE
TODAY HELLO CAR
HELLO ZEBRA LION DON
TYPE BABY

我得到(不带引号)

"HI"
"BABY"
"TYPE" 
"MAYBE
TODAY"
4

5 回答 5

3

看这个:

if(c != ' ') {
    // ...
} else if(c == '\n') {
    // WILL NEVER BE REACHED
}

如果c == '\n', thenc != ' '也是true,这意味着将跳过第二个块,并且将针对所有'\n'字符运行第一个块(即它们将被打印)。

关于行尾的其他答案是错误的。未以二进制模式打开的CFILE *将为您处理 EOL。如果你有一个来自 DOS 的文件并且你在 Unix 上阅读它可能会产生问题,但我怀疑这是你的问题,如果它正在处理它可能比这里显示的答案更复杂一些。但是当你到达它时,你可以穿过那座桥。

于 2012-01-14T22:07:07.160 回答
1

行终止字符的编码因操作系统而异。在 Linux 中,它只是 '\n',而在 Windows 和 DOS 中,它是 '\r\n'。因此,根据您的目标操作系统,您可能需要更改您的声明,例如:

if((c == '\r' || (c == '\n'))
{
   //...
}

编辑:仔细观察后,我认为您做错的是即使您阅读了\ n,第一个if语句也是正确的,因此您应该这样处理:

if((c != ' ') && (c != '\n')){
    printf("%c", c);
    line[i++] = c;
}
else if((c == '\n') || (c == '\r')){

//this is where It should do nothing

}
else{
   //...
}
于 2012-01-14T22:00:48.197 回答
0

尝试这个;

 if((c == '\n') || (c == '\r'){ 
于 2012-01-14T21:54:23.960 回答
0

这对我有用(在 Linux 上):

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

int
main(int argc, char **argv)
{
        char c;
        size_t i = 0;
        FILE *file = NULL;
        char buffer[BUFSIZ];
        int status = EXIT_SUCCESS;
        if (argc < 2) {
                fprintf(stderr, "%s <FILE>\n", argv[0]);
                goto error;
        }
        file = fopen(argv[1], "r");
        if (!file) {
                fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1],
                                strerror(errno));
                goto error;
        }
        while (EOF != (c = fgetc(file))) {
                if (BUFSIZ == i) {
                        fprintf(stderr, "%s: D'oh! Write a program that "
                                        "doesn't use static buffers\n",
                                        argv[0]);
                        goto error;
                }
                if (' ' == c || '\n' == c) {
                        buffer[i++] = '\0';
                        fprintf(stdout, "%s\n", buffer);
                        i = 0;
                } else if ('\r' == c) {
                        /* ignore */
                } else {
                        buffer[i++] = c;
                }
        }
exit:
        if (file) {
                fclose(file);
        }
        return status;
error:
        status = EXIT_FAILURE;
        goto exit;
}
于 2012-01-14T22:09:47.150 回答
0

更改 if( c != ' ')if( c != ' '&&c!='\n')

这应该可以解决问题

于 2012-01-14T22:16:36.463 回答