小编Aqo*_*Aqo的帖子

当我认为应该在 linux 上时 c fputc 不会返回错误,在 Windows 上按预期工作

我正在尝试使用 stdio.h 中的 c 库函数 fputc

我假设它应该根据https://linux.die.net/man/3/fputc上的规范工作

具体来说,感兴趣的部分是:

  • fputc() 将转换为无符号字符的字符 c 写入流。
  • fputc()、putc() 和 putchar() 将作为无符号字符写入的字符在错误时返回为 int 或 EOF。

根据这些信息,我假设如果 fputc 成功将字符写入提供的流,我应该收到一个等于写入字符的返回值,如果写入流失败,我应该得到 EOF 的值。

这是我的代码:

// COMPILE
//    gcc -Wall -Wextra -Werror -O3 -s ./fputc.c -o ./fputc-test
// RUN
//    ./fputc-test

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

void print_error_exit();

int main() {
    FILE * fp;
    
    // ensure file exists
    if((fp = fopen("file", "w")) == NULL) print_error_exit();
    
    // close stream
    if(fclose(fp) == EOF) print_error_exit();
    
    // open file …
Run Code Online (Sandbox Code Playgroud)

c linux gcc

2
推荐指数
2
解决办法
89
查看次数

标签 统计

c ×1

gcc ×1

linux ×1