小编Luc*_*ucy的帖子

文件描述符错误

我正在学习文件描述符,我写了这段代码:

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

int fdrd, fdwr, fdwt;
char c;

main (int argc, char *argv[]) {

    if((fdwt = open("output", O_CREAT, 0777)) == -1) {
        perror("Error opening the file:");
        exit(1);
    }

    char c = 'x';

    if(write(fdwt, &c, 1) == -1) {
        perror("Error writing the file:");
    }

    close(fdwt);
    exit(0);

}
Run Code Online (Sandbox Code Playgroud)

,但我得到了: Error writing the file:: Bad file descriptor

我不知道会出现什么问题,因为这是一个非常简单的例子.

c unix file file-descriptor

19
推荐指数
3
解决办法
7万
查看次数

标签 统计

c ×1

file ×1

file-descriptor ×1

unix ×1