相关疑难解决方法(0)

为什么"while(!feof(file))"总是错的?

我看到人们最近在很多帖子中试图读取这样的文件.

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

int
main(int argc, char **argv)
{
    char *path = argc > 1 ? argv[1] : "input.txt";

    FILE *fp = fopen(path, "r");
    if( fp == NULL ) {
        perror(path);
        return EXIT_FAILURE;
    }

    while( !feof(fp) ) {  /* THIS IS WRONG */
        /* Read and process data from file… */
    }
    if( fclose(fp) == 0 ) {
        return EXIT_SUCCESS;
    } else {
        perror(path);
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个__CODE__循环有什么问题?

c file while-loop feof

550
推荐指数
4
解决办法
21万
查看次数

避免在printf()中尾随零

我一直绊倒printf()函数系列的格式说明符.我想要的是能够在小数点后打印一个最大给定位数的double(或float).如果我使用:

printf("%1.3f", 359.01335);
printf("%1.3f", 359.00999);
Run Code Online (Sandbox Code Playgroud)

我明白了

359.013
359.010
Run Code Online (Sandbox Code Playgroud)

而不是期望的

359.013
359.01
Run Code Online (Sandbox Code Playgroud)

有谁能够帮我?

c printf

105
推荐指数
4
解决办法
11万
查看次数

标签 统计

c ×2

feof ×1

file ×1

printf ×1

while-loop ×1