Article View: comp.os.msdos.programmer
Article #7921Re: open vs fopen?
From: Andreas Karnetzki
Date: Wed, 21 Feb 1996 22:18
Date: Wed, 21 Feb 1996 22:18
66 lines
2908 bytes
2908 bytes
sherlock@micronet.fr schrieb: > natewild@mbnet.mb.ca (Nathan T. Wild) wrote: > >In C, why would one use open and DOS int handles rather than fopen and > >stdio file handles? > >Is there some speed advantage or differnet functionality? > because files opened using open and DOS files handles can be read and > write at the same time, while stdio files can't. That's not correct, I think. You can open files for read/write with the the stdio routines, too. Just use the "w+", "r+" or "a+" open modes with fopen(). fopen( "New.dat", "w+" ); opens a file starting with 0 bytes for writing but with the possibility to switch to read mode without the need of closing it. fopen( "Old.dat", "r+" ); opens an (existing) file starting with reading with the possibility to switch to writing without closing. fopen( "Any.dat", "a+" ); opens for appending and maybe reading afterwards. The only thing to assure for read/write switches is to use an fseek() call between read and write operations: fp = fopen( "My.dat", "r+" ); fwrite( "Hello", 1, 5, fp ); fseek( fp, 0L, SEEK_SET ); fread( Message, 1, 5, fp ); fseek( fp, 0L, SEEK_CUR ); fwrite( " and Goodby.", 1, 12, fp ); fclose( fp ); The main difference between stdio calls and the "low-level" routines (not only under DOS but for Unix too) is the absence of buffering in the low-level functions. if you use fwrite(), for example, you can not be sure that the number of bytes you want to write are actually written to the output at that time. The "real" write operation will take place if the stdio buffer for that file is full. This can or can not be an advantage for your application. In some cases you know better when it is the right time to write (or read) and then the stdio functions contain unneccessary overhead. In other cases the implemented buffering of stdio input/output will make it unneccessary to (re)implement an effectiv read/write functionality (I think no one would like to start the HD motor 1000 times just to write 1000 characters). But there is another point to keep in mind. The low-level routines aren't as portable as the stdio functions. Each system has its own set of functions. ---- Andreas Karnetzki, Munich, Germany. Email: medardus@cube.net, CompuServe: 74001,1534
Technical Details
Message-ID:
<4gg5nk$he1@salyko.cube.net>Path:
arc1-ca.pugleaf.net!news.cube.net!medardusReferences:
<uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8qbg$549@chleuasme.francenet.fr>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.cube.net!medardus From: medardus@cube.net (Andreas Karnetzki) Newsgroups: comp.os.msdos.programmer,comp.lang.c Subject: Re: open vs fopen? Followup-To: comp.os.msdos.programmer,comp.lang.c Date: 21 Feb 1996 22:18:28 GMT Organization: CUBENet Multiline BBS Message-ID: <4gg5nk$he1@salyko.cube.net> References: <uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8qbg$549@chleuasme.francenet.fr> NNTP-Posting-Host: cubnt6.cube.net Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: TIN [version 1.2 PL0]