免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5264 | 回复: 5
打印 上一主题 下一主题

fopen 和 open的不同?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-17 06:29 |只看该作者 |倒序浏览
  1. int open (const char *pathname, int flags, mode_t mode);
  2. FILE *fopen (const char *path, const char *mode);
复制代码


open的宣告是int, 而fopen的宣告是FILE,
我想問一下他們有什麼不同??
再問一下使用fopen會不會比open使用更多的內存??

论坛徽章:
0
2 [报告]
发表于 2005-11-17 08:31 |只看该作者
这个还是看书吧,一个是系统调用,一个是库函数实现,估计又要被封贴了

论坛徽章:
0
3 [报告]
发表于 2005-11-17 10:31 |只看该作者
fopen有缓存, open直接读硬盘

论坛徽章:
0
4 [报告]
发表于 2005-11-17 10:54 |只看该作者
最好先查一下以前的帖子,以前的帖子中是这样写的。
open和fopen的区别:
前者属于低级IO,后者是高级IO。
前者返回一个文件描述符,后者返回一个文件指针。
前者无缓冲,后者有缓冲。
前者与 read, write 等配合使用, 后者与 fread, fwrite等配合使用。
后者是在前者的基础上扩充而来的,在大多数情况下,用后者。

论坛徽章:
0
5 [报告]
发表于 2005-11-19 16:46 |只看该作者

有无缓冲的区别

有无缓冲的区别

论坛徽章:
0
6 [报告]
发表于 2005-11-20 10:34 |只看该作者
fopen的一个实现

#if        defined(_POSIX_SOURCE)
#include        <sys/types.h>
#endif
#include        <stdio.h>
#include        <stdlib.h>
#include        "loc_incl.h"

#define        PMODE                0666

/* The next 3 defines are true in all UNIX systems known to me.
*/
#define        O_RDONLY        0
#define        O_WRONLY        1
#define        O_RDWR                2

/* Since the O_CREAT flag is not available on all systems, we can't get it
* from the standard library. Furthermore, even if we know that <fcntl.h>
* contains such a flag, it's not sure whether it can be used, since we
* might be cross-compiling for another system, which may use an entirely
* different value for O_CREAT (or not support such a mode). The safest
* thing is to just use the Version 7 semantics for open, and use creat()
* whenever necessary.
*
* Another problem is O_APPEND, for which the same holds. When "a"
* open-mode is used, an lseek() to the end is done before every write()
* system-call.////////////////////////////////////////////////////////////////////////////////////////////////////////
*
* The O_CREAT, O_TRUNC and O_APPEND given here, are only for convenience.
* They are not passed to open(), so the values don't have to match a value
* from the real world. It is enough when they are unique.
*/
#define        O_CREAT                0x010
#define        O_TRUNC                0x020
#define        O_APPEND        0x040

int _open(const char *path, int flags);
int _creat(const char *path, Mode_t mode);
int _close(int d);

FILE *
fopen(const char *name, const char *mode)
{
        register int i;
        int rwmode = 0, rwflags = 0;
        FILE *stream;
        int fd, flags = 0;

        for (i = 0; __iotab[i] != 0 ; i++)
                if ( i >= FOPEN_MAX-1 )
                        return (FILE *)NULL;

        switch(*mode++) {
        case 'r':
                flags |= _IOREAD | _IOREADING;       
                rwmode = O_RDONLY;
                break;
        case 'w':
                flags |= _IOWRITE | _IOWRITING;
                rwmode = O_WRONLY;
                rwflags = O_CREAT | O_TRUNC;
                break;
        case 'a':
                flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
                rwmode = O_WRONLY;
                rwflags |= O_APPEND | O_CREAT;
                break;         
        default:
                return (FILE *)NULL;
        }

        while (*mode) {
                switch(*mode++) {
                case 'b':
                        continue;
                case '+':
                        rwmode = O_RDWR;
                        flags |= _IOREAD | _IOWRITE;
                        continue;
                /* The sequence may be followed by additional characters */
                default:
                        break;
                }
                break;
        }

        /* Perform a creat() when the file should be truncated or when
         * the file is opened for writing and the open() failed.
         */
        if ((rwflags & O_TRUNC)
            || (((fd = _open(name, rwmode)) < 0)
                    && (rwflags & O_CREAT))) {
                if (((fd = _creat(name, PMODE)) > 0) && flags  | _IOREAD) {
                        (void) _close(fd);
                        fd = _open(name, rwmode);
                }
                       
        }

        if (fd < 0) return (FILE *)NULL;

        if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) {
                _close(fd);
                return (FILE *)NULL;
        }

        if ((flags & (_IOREAD | _IOWRITE))  == (_IOREAD | _IOWRITE))
                flags &= ~(_IOREADING | _IOWRITING);
        stream->_count = 0;
        stream->_fd = fd;
        stream->_flags = flags;
        stream->_buf = NULL;
        __iotab[i] = stream;
        return stream;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP