不寻常的文件输出

时间:2014-09-14 15:33:58

标签: c file io

这是我读取文件的代码:

void readNote() {
    FILE* file = fopen("records.txt", "r"); /* should check the result */
    char line[256];

    while (fgets(line, sizeof(line), file)) {
        int i = 0;
        while(line[i] != ' ') {
            printf("%c", line[i]);
            i++;
        }
    }

    fclose(file);
}

这是我的档案:

December 20: Note 1



March 21: Note 2



March 4: My note 1



June 23: Note 2 happy



January 3: Birthday



January 3: Birthday

这是我运行我的功能时的输出:

#     @Γ⌂è∙  ≈áA     [3wè∙
#     @Γ⌂è∙  ≈áA     [3wè∙
#     @Γ⌂è∙  ≈áA     [3wè∙  December
cember
cember
cemberMarch
rch
rch
rchMarch
rch
rch
rchJune
ne
ne
neJanuary
nuary
nuary
nuaryJanuary

为什么这些不寻常的角色会突然冒出来?

1 个答案:

答案 0 :(得分:1)

尝试

更改

while(line[i] != ' ') {

while(line[i] != ' ' && line[i] != '\n' && line[i] != '\0') {