fopen与_open什么区别?
各有什么有缺点?多谢! 问题点数:20、回复次数:3Top
1 楼friarchen(山间有雾)回复于 2002-06-20 20:56:25 得分 7
相同点就是都是对文件打开进行操作的。
不同点有:
1. open是系统调用,fopen是函数调用
2. open是不带缓冲的,fopen是带缓冲的
3. open频繁操作时速度慢,fopen操作后需要刷新才更新数据Top
2 楼bbird(随风鸟)回复于 2002-06-20 20:57:54 得分 5
FILE *fopen( const char *filename, const char *mode );
int _open( const char *filename, int oflag [, int pmode] );
用open一般跟fstat函数可以一次将文件的类容搞定。
int bsz;
struct stat fst;
char *cbuf;
int fd, rc;
fd = open(filename, O_RDONLY, 0);
rc = fstat(fd, &fst);
cbuf = (char *)calloc(1, fst.st_size + 1);
bsz = read(fd, cbuf, fst.st_size);
cbuf[bsz]='\0';
close(fd);
很快就将文件类容读入到cbuf中了。(判断语句自己加吧:))
Top
3 楼batizhou(batizhou)回复于 2002-06-20 21:00:17 得分 8
没有太大的区别,也没有什么缺点
fopen返回的是FILE* which is a stream
_open返回的是int which is a runtime file handle
你可以:
fd = _open(...);
FILE * f = _fdopen(fd, ...);
效果和fopen一样
_open的出现是为了兼容UNIX的POSIX标准
Top




