错误的文件描述符 [英] Bad file descriptor

查看:976
本文介绍了错误的文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习的文件描述符,我写了这个code:

I'm learning about file descriptors and I wrote this code:

#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);

}

,但我发现:错误写入文件::错误的文件描述符

我不知道什么可能是错的,因为这是一个很简单的例子。

I don't know what could be wrong, since this is a very simple example.

推荐答案

试试这个:

open("output", O_CREAT|O_WRONLY, 0777)

这篇关于错误的文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆