搜索
c - Linux 文件 IO 的一个问题
怪我咯
怪我咯 2017-04-17 13:03:34
[Linux讨论组]

在下面的代码中:

#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!

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

先谢谢大家了!

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
巴扎黑

第一个问题:你需要在 open 的第二参数里加上 O_WRONLY 才行。另外,creat 相当于 open 的时候指定了 O_CREAT|O_WRONLY|O_TRUNC 标记。

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号