fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf

上传人:tbuqq 文档编号:5616008 上传时间:2020-07-02 格式:PDF 页数:10 大小:89.47KB
返回 下载 相关 举报
fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf_第1页
第1页 / 共10页
fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf_第2页
第2页 / 共10页
fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf_第3页
第3页 / 共10页
fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf_第4页
第4页 / 共10页
fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf》由会员分享,可在线阅读,更多相关《fwrite,write,fread,read,fopen,open函数的区别及效率.doc.pdf(10页珍藏版)》请在三一文库上搜索。

1、fwrite/write, fread/read, fopen/open 函数的区别及效率? X.比较 1)fwirte/fread:是带缓冲的, vvrite/read:是不带缓冲的 2 ) fopen是标准 c 里定义的,不能指定要创建文件的权限,fopen返回指针。 open是 POSIX中定义的,可以指定权限,返回文件描述符(整数)。 2.实例说明: 如果文件的大小是8k0 你如果用 read/write,且只分配了2k 的缓存,则要将此文件读出需要做4 次系统调用来实际 从磁盘上读出。如果你用fread/fwrite,则系统自动分配缓存,则读出此文件只要一次系统调用 从磁盘上读出。也

2、就是用read/write要读 4 次磁盘,而用 fread/fwrite则只 要读 1 次磁盘。 效率比 read/write要高 4 倍。 如果程序对内存有限制,则用read/write比较好。 都用 fread 和 fwrite, 它自动分配缓存,速度会很快,比自己来做要简单。如果要处理一些特殊 的描述符,用 read 和 write, 如套接口,管道之类的 系统调用 write的效率取决于你 buf 的大小和你要写入的总数量,如果buf 太小,你进入内核 空间的次数大增,效率就低下。而fwrite会替你做缓存,减少了实际出现的系统调用, 所以效 率比较高。 如果只调用一次(可能吗?),

3、这俩差不多,严格来说write要快一点点(因为实际上fwrite 最 后还是用了write做真正的写入文件系统工作),但是这其中的差别无所谓。 2. fwrite和fread的源代码参考源码:(Microsoft的源代码,在 vccrtsrcfwrite.c 里) / 【 【 【 【 【 【 【 【* 【 【 【 【 【 【 【 【 7“ 7“ 7“斗 “7“ 斗 “斗“ 斗“ 斗“ 斗 “7“ 斗“ / * 、 * 、* 、* 、* 、* 、* 、*A、*A、 * 、 * 、 * 、 * 、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、*A、

4、* 、*A、*A、*A、*A、*A、*A、* 、*A、*A、*A、*A、* 、*A、*A、*A、*A、 * 、*A、*A、*A、*A、*A、*A、*A、 S、彳?彳、彳、叫彳、彳彳、叫、 彳?彳? % 、 % 、 *fwrite.c - read from a stream *Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved. * Purpose: * Write to the specified stream from the users buffer. * #include #include #inclu

5、de #include #include #include #include *size_t fwrite(void buffer, size_t size, size_t count, FILE * stream)- *write to the specified stream from the specified buffer. * Purpose: *Write rcountf items of size size to the specified stream from *the specified buffer. Return when count items have been w

6、ritten *or no more items can be written to the stream. * *Entry: * buffer -pointer to users buffer * size -size of the item to write * count -number of items to write * stream -stream to write to *Exit: Returns the number of (whole) items that were written to the stream. This may be less than count

7、if an error or eof occurred. In this case, ferror() or feof() should be used to distinguish between the two conditions. *Notes: * fwrite will attempt to buffer the stream (side effect of the _flsbuf * * * * * * call) if necessary. No more than OxFFFE bytes may be written out at a time by a call to w

8、rite(). Further, write() does not handle huge buffers. Therefore, in large data models, the write request is broken down into chunks that do not violate these considerations- Each of these chunks is * * * * * processed much like an fwrite() call in a small data model (by a call to _nfwrite(). This c

9、ode depends on _iob being a near array. * * * * MTHREAD/DLL - Handled in just two layers since it is small data model. The outer layer, fwrite(), handles stream locking/unlocking and calls _fwrite_lk() to do the work. _fwrite_lk() is the same as the single-thread, small data model version of fwrite(

10、). * KJ“ KJ“ / / #ifdef_MT /* define locking/unlocking version */ size_t _cdecl fwrite ( const void * buffer, size_t size, size_t count, FILE * stream ) size_t retval; _lock_str(stream); /* lock stream */ retval = _fwrite_lk(buffer5 size, count, stream); /* do the read */ _unlock_str(stream); /* unl

11、ock stream */ return retval; #end讦 /* MT*/ /* define the normal version */ #ifdef_MT size_t _cdecl _fwritek ( #else /* _MT */ size_t _cdecl fwrite ( #endif /* _MT */ const void * buffer, size_t size, size_t num, FILE * stream) const char *data; unsigned total; unsigned count; unsigned bufsize; unsig

12、ned nbytes; unsigned nwritten; int c; /* point to where data comes from next */ /* total bytes to write */ /* num bytes left to write */ /* size of stream buffer */ /* number of bytes to write now */ /* number of bytes written */ /* a temp char */ /* initialize local vars */ data = buffer; count = t

13、otal = size * num; if (0 = count) return 0; if (anybuf(stream) /* already has buffer, use its size */ bufsize = stream-_bufsiz; else #if defined (_M_M68K) | defined (_M_MPPC) /* assume will get BUFSIZ buffer */ bufsize = BUFSIZ; #else /* defined (_M_M68K) | defined (_M_MPPC) */ /* assume will get _I

14、NTERNAL_BUFSIZ buffer */ bufsize =NTERNAL_BUFSIZ; #endif 产 defined (_M_M68K) | defined (_M_MPPC) */ /* here is the main loop we go through here until were done */ while (count != 0) /* 如果缓冲区还有空间, 拷贝数据到缓冲区。if the buffer is big and has room, copy data to buffer */ if (bigbuf(stream) memcpy(stream-_ptr

15、, data, nbytes); /* update stream and amt of data written */ count -= nbytes; stream-_cnt -= nbytes; stream-_ptr += nbytes; data += n bytes; else if (count = bufsize) /* 如果发送数据量大于默认缓冲区的大小,写数据时调用“write ”函数 来写 “默认缓冲区大小”整数倍的字节数, If we have more than bufsize chars to write, write data by calling write w

16、ith an integral number of bufsiz blocks. If we reach here and we have a big buffer, it must be full so _flush it. */ if (bigbuf(stream) if (_flush(stream) /* eiTor, stream flags set weTe out of here */ return (total ? count) / size; /* calc chars to read 一(count/bufsize) * bufsize */ nbytes = ( bufs

17、ize ? (count ? count % bufsize): counl); nwritten = _write(_fileno(stream), data, nbytes); if (nwritten = (unsigned)EOF) /* eiTor out of here */ stream- Hag |= _IOERR; return (total - count) / size; /* update count and data to reflect write */ count -= nwritten; data += nwritten; if (nwritten _flag

18、|= _IOERR; return (total ? count) / size; else /* 缓冲区满并且没有足够的字符来直接写,则用_flsbuf*/ /* buffer full and not enough chars to do direct write, so do a _flsbuf. */ c = * data; /* _flsbuf write one char, this is it */ if (_flsbuf(c, stream) = EOF) /* eiror or eof, stream flags set by _flsbuf */ return (total

19、 ? count) / size; /* _flsbuf wrote a char update count */ +data; ? -count; /* update buffer size */ bufsize = stream-_bufsiz 0 ? stream-_bufsiz : 1; /* we finished successfully, so just return num */ return num; / f ?丫? ?丫? rJ* rj* rj* ?丫? ?丫? ?丫? rj* ?丫?丫?丫?丫?丫? ?丫? ?丫? rJ* ? 丫? ?卜 ?丫?丫? ?丫?丫? ?丫?

20、?丫?丫?丫? * fread.c - read from a stream * Copyright (c) 1989-1997, Microsoft Corporation? All rights reserved. * Purpose: Read from the specified stream into the users buffer. ?. 、?. 、?. 、?. 、?. 、?. 、 f ?. 、?. 、 ?.、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、 ?. 、?. 、?. 、?

21、. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、 ?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、?. 、 #include #include #include #include #include #include /* *size_t fread(void * buffer, size_t size, size_t count, FILE * stream)- *read from specifi

22、ed stream into the specified buffer. * * Purpose: Read counf items of size size from the specified stream into the specified buffer. Return when count items have been read in or no more items can be read from the stream. ? pointer to users buffer ?size of the item to read in -number of items to read

23、 -stream to read from *Exit: Returns the number of (whole) items that were read into the buffer. This may be less than ount 1 if an error or eof occurred. In this case, ferror() or feof() should be used to distinguish between the two conditions. *Notes: fread will attempt to buffer the stream (side

24、effect of the _filbuf call) if necessary. No more than OxFFFE bytes may be read in at a time by a call to read(). Further, read() does not handle huge buffers. Therefore, in large data models, the read request is broken down into chunks that do not violate these considerations. Each of these chunks

25、is processed much like an fread() call in a small data model (by a call to _nfread(). MTHREAD/DLL - Handled in three layers. fread() handles the locking means * mu 6iunu j_0zis OZIS Ozis “gjjnq* piOA *Entry: * buffer size count stream /* 丄凶一 */ J!PU。# )pBQJJ |99p0 )_0ZIS /* 1W _ */ os*# )” ppgjj joo

26、po J-0ZIS 1WFPJ!# /* UOISJOA IUUIJOU。屮9UIJ9p */ /*UAT*/ J!PU0# :FAQI UJDJ9J /* UJU0J1S poun */ t(UIUOJ)S)J) s _ooiun_ /* puoj op */ t(iuu0j ) s 4iunoo 4ozis 冇 0Jjnq) ” ppay二EAIOJ /* UIV0JJS po */ :(iuEa【)s) HS MAQ 1_9ZIS ( UIEOHS* 31H 4iunoo 厂 ozp 6OZIS _9ZIS ? ? Mjjnq* P!OA )pUOJJ |30po J-0ZIS /* U

27、OISJ0A Sui30|un/ui100| ouijop */ 1WFPJ!# / / 吩? rj* 吟?rj? rj? rjw rjw rjw rJw ? 、?、?、?、?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.? ?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?. 、?. 、 ?()pE9JJ JO UOISJOA pP0jq ) -9I UTS。屮SE OUJES 0屮SI () ”pPOJJ ?” JOM PnjOP。屮Op 01 ()” pEOJJ S(IP9 p

28、UE Suiooj UJP9J1S。屮jo ojpo so”E()pEay ioA引JOJUO oq丄?ppoui uwp ECUS si u 90UIS SJQAEI 0M2屮 isnf UI poipuH ? nna/avaHHlW/98 ?”JOM (unjoB。屮op oi Opuojju - SEOpun s ”untp oiqpsoSip OJUI ;sonboj pEOj。 屮dn syjpojq 开? ()pE9目jo UOISJOA opoui njnp OSJUJ “pF?屮 V分iqs。 屮su ouius。屮si () ”厂pea厂? ”IOA。屮op o)() ”pe

29、a厂S【EO puu (pojinbsj ji) uuoiS0j/uipuo|/uiAUS sa pum /* point to where should be read next */ /* total bytes to read */ /* num bytes left to read */ /* size of stream buffer */ /* how much to read now */ /* how much we did read */ /* a temp char */ /* initialize local vars */ data = buffer; if (coun

30、t = total = size * num) = 0 ) return 0; if (anybuf(stream) /* already has buffer, use its size */ bufsize = strea _bufsiz; else #if defined (_M_M68K) | defined (_M_MPPC) /* assume will get BUFSIZ buffer */ bufsize = BUFSIZ; #else /* defined (_M_M68K) | defined (_M_MPPC) */ /* assume will get _INTERN

31、AL_BUFSIZ buffer */ bufsize = _INTERNAL_BUFSIZ; #endif /* defined (_M_M68K) | defined (_M_MPPC) */ /* here is the main loop we go through here until were done */ while (count != 0) /* if the buffer exists and has characters, copy them to user buffer */ if (anybuf(stream) memcpy(data, stream-_ptr, nb

32、ytes); /* update stream and amt of data read */ count -= nbytes; strearn-_cnt -= nbytes; stream-_ptr += nb ytes; data += nbytes; else if (count = bufsize) /* If we have more than bufsize chars to read, get data by calling read with an integral number of bufsiz blocks. Note that if the stream is text

33、 mode, read will return less chars than we ordered. */ char * data; unsigned total; unsigned count; unsigned bufsize; unsigned nbytes; unsigned nread; int c; /* calc chars to read (count/bufsize) * bufsize */ nbytes = ( bufsize ? (count - count % bufsize): count); nread = _read(_fileno(stream), data

34、, nbytes); if (nread = 0) /* end of file out of here */ stream-_flag |=OEOF; return (total - count) / size; else if (nread = (unsigned)-!) /* error out of here */ stream-_flag |= _IOERR; return (total ? count) / size; /* update count and data to reflect read */ count -= nread; data += nread; else /*

35、 less than bufsize chars to read, so call _filbuf to fill buffer */ if (c = _filbuf(stream) = EOF) /* error or eof, stream flags set by _filbuf */ return (total - count) / size; /* _filbuf returned a char store it */ *data+ = (char) c; count; /* update buffer size */ bufsize = stream-_bufsiz; /* we finished successfully, so just return num */ return num;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1