开发者社区> 问答> 正文

Linux 文件 IO 的一个问题

在下面的代码中:在下面的代码中:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define STDOUT 1

char *file_name;

int newfile(char *path)
{
        int fd;
        mode_t mode = S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH;

        if ((fd = creat(path, mode)) < 0) {
                fprintf(stderr, "%s: %s: fail to create new file: %s",
                        file_name, path, strerror(errno));
                return -1;
        }

        return fd;
}

int read2file(int fd)
{
        ssize_t wcount;
        ssize_t rcount;
        char buf[BUFSIZ] = {0};

        fprintf(stdout, "Please inpu the content: ");
        fprintf(stdout, "%d\n", fd);
        while ((rcount = read(STDOUT, buf, sizeof(buf))) > 0) {
                wcount = write(fd, buf, rcount);
                if (rcount != wcount) {
                        fprintf(stderr, "%s: fail to write: %s\n",
                                file_name, strerror(errno));
                        return 1;
                }
        }
        close(fd);
        return 0;
}

int main(int argc, char **argv)
{
        char path[BUFSIZ];
        int fd;
        file_name = argv[0];

        printf("Please input the new file path: ");
        /*getchar();*/
        scanf("%s", path);
        fd = newfile(path);
        if (fd < 0) {
                return -1;
        }
        read2file(fd);

        return 0;
}
如果把 newfile() 中的 creat(path, mode) 换成 open(path, O_CREAT, mode) , 就会报错:

Bad file description
但是,两个函数返回的文件描述符都是 3 。

究竟是为什么?

还有,代码的执行结果有一个问题,就是在 read2file() 函数中的

 fprintf(stdout, "Please inpu the content: ");
如果不加上后面这一句:

 fprintf(stdout, "%d\n", fd);
那么执行的结果就是:

Please input the new file path: ./2
Hello, world!
Please inpu the content: %
这显然是不正常的。但是如果加上后面的那一句 fprintf(stdout, "%d\n", fd); 那么一切就都正常了:

Please input the new file path: ./2
Please inpu the content: 3
Hello, world!

究竟是为什么到时输出的不正常?

先谢谢大家了!

展开
收起
a123456678 2016-06-21 11:30:18 1950 0
1 条回答
写回答
取消 提交回答
  • 第一个问题:你需要在 open 的第二参数里加上 O_WRONLY 才行。另外,creat 相当于 open 的时候指定了 O_CREAT|O_WRONLY|O_TRUNC 标记。

    第二个问题:你需要 fflush(stdout)。

    另外,read(STDOUT, buf, sizeof(buf)) 这句话想干嘛,从 STDOUT 读输入么?而且值定义成了 2,这个应该是 STDEER 才对呢。

    2019-07-17 19:44:41
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载