小编Lan*_*don的帖子

返回fprintf的值

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(void)
{
    FILE *fh = fopen ("file.txt", "w");
    if (fh != NULL) {
        int i = 0;
        while(i < 5){
            if (fprintf (fh, "%s", "hello") < 0)
            {
                fprintf (stderr, "err=%d: %s\n", errno, strerror (errno));
            }
            if(ferror(fh))
            {
                printf("wrong\n");
            }
            sleep(10);
        }
    }  
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

当我在程序运行期间删除文件"file.txt"时,我希望fprintf返回小于0的值以打印错误消息.但是fprintf总是返回"hello"字符串的大小,即5.

注意:由于有10秒的睡眠调用,我在程序终止之前删除了该文件.

如果fprintf/fwrite写入不再存在的文件(由文件描述符指向),请说明如何生成错误消息.

c printf fwrite

-1
推荐指数
1
解决办法
1105
查看次数

标签 统计

c ×1

fwrite ×1

printf ×1