相关疑难解决方法(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万
查看次数

getchar/fgetc和putchar/fputc中int和char的区别?

我正在尝试自己学习C,我有点困惑getcharputchar:

1

#include <stdio.h>

int main(void)
{
    char c;
    printf("Enter characters : ");
    while((c = getchar()) != EOF){
      putchar(c);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

2

#include <stdio.h>

int main(void)
{
    int c;
    printf("Enter characters : ");
    while((c = getchar()) != EOF){
      putchar(c);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

C库函数int putchar(int c)将参数char指定的字符(unsigned char)写入stdout.

C库函数int getchar(void)从stdin获取一个字符(一个unsigned char).这相当于以stdin作为参数的getc.

这是否意味着putchar()同时接受intchar或其中一方以及getchar()我们应该使用一个intchar

c

25
推荐指数
1
解决办法
5595
查看次数

标签 统计

c ×2

feof ×1

file ×1

while-loop ×1