- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Difference between open and fopen
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2007 05:28 PM
тАО09-23-2007 05:28 PM
Difference between open and fopen
Can any one explain the exact difference between open and fopen, read and fread, write and fwrite ?
-Masthan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2007 05:43 PM
тАО09-23-2007 05:43 PM
Re: Difference between open and fopen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2007 06:12 PM
тАО09-23-2007 06:12 PM
Re: Difference between open and fopen
Are you interested in performance? Or portability?
Are you doing seeks?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2007 06:15 PM
тАО09-23-2007 06:15 PM
Re: Difference between open and fopen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2007 07:15 PM
тАО09-23-2007 07:15 PM
Re: Difference between open and fopen
It depends on whether you need the buffering or not. Whether you need to process the data and need fancy fscanf and fprintf or just binary access.
Whether you are using sequential access or random and the size of your requests. If copying a file, just use system calls with Mb sized buffers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 02:44 AM
тАО09-24-2007 02:44 AM
Re: Difference between open and fopen
Essentially the 'f' functions do a lot of the work that you would do to write efficient code at the expense of losing some measure of control.
Originally UNIX allowed 20 open files per process. Later the number of file descriptoes was increased (usually to 64) but many flavors of UNIX still only allowed 20 high-level *FILE's so that the 'f' functions could only be used if the file descriptor were 19 or less. Later this limitation disappeared as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 06:01 AM
тАО09-24-2007 06:01 AM
Re: Difference between open and fopen
So, unless you want to write and handle EVERYTHING yourself, use fopen() with the FILE type, otherwise, use open() for a more low level do it yourself approach.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 06:53 PM
тАО09-24-2007 06:53 PM
Re: Difference between open and fopen
fwrite()/fprintf() are buffered at user level, so if you need to be sure data are flushed from user level buffer to the file at some point, you MUST use fflush().
Same for reading, if you have a random access file which may be changed and read at the same time by multiple programs, you may prefer to avoid the use of user level buffer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 11:13 PM
тАО09-24-2007 11:13 PM
Re: Difference between open and fopen
Thanks for your response. But unfortunate thing is still I am not convionced with your answers. Yes, you all are correct. fopen/fread/fwrite are buffered and open/read/write are unbuffered. What benifit we will get with buffered IO over Unbuffered IO.
When we have open/read/write calls, why should we go for fopen/fread/fwrite calls ?
is fopen/fread/fwrite offering any special benifit over open/read/write ? If so, what it is ?
-Masthan D
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-25-2007 01:23 AM
тАО09-25-2007 01:23 AM
Re: Difference between open and fopen
be a good thing? (Yikes.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-25-2007 01:38 AM
тАО09-25-2007 01:38 AM
Re: Difference between open and fopen
How about taking a class on data structures, to see what is out there in the world for you. Find out what a circular queue is, also what a link list is, what a hash is, learn some graphing theory, you might even check out chapter two, buffers. Feel free to think beyond what the book says that they are useful for, and perhaps envision what more you can do with them and think of ways to extend their abilities beyond the teachings of the book and the class...
...
Then, go right ahead and doubt that any thing you've learned is of any use, and never agree to use them unless someone somehow convinces you that they are any good.
Really, buffering is used to soften the effects of ineffective delays caused by hardware, algorighthms, heuristics, or analysis, and to provide the ability to do analysis and manipulation of data beyond a single unit of data: in our world, a byte.
Examples of a buffer:
cache in the cpu, on the mother board - to soften the hardware blow of reaching commonly used data from distant ram.
Disk cache in a program or an OS: to soften the blow of reaching for data directly from disk.
In your case, buffering is used to soften the blow of byte-at-a-time I/O and provide extended use of I/O streams capable of data manipulation approaching that of the data types being used by the language (not be confused with true object definition, but coming close in a sense).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-25-2007 02:13 AM
тАО09-25-2007 02:13 AM
Re: Difference between open and fopen
You need to answer our questions. What are your operations and sizes?
>When we have open/read/write calls, why should we go for fopen/fread/fwrite calls?
Are you happy with the performance? If you already have the former, then tusc will tell you exactly what you are doing.
>is fopen/fread/fwrite offering any special benefit over open/read/write?
If your sizes are trivial, then you want buffering.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-25-2007 02:37 AM
тАО09-25-2007 02:37 AM
Re: Difference between open and fopen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-26-2007 02:18 AM
тАО09-26-2007 02:18 AM
Re: Difference between open and fopen
Basically if you make sequential access to a file, and want to get it pieces by pieces, use FILE/f*. It will optimize the syscalls, getting big chuncks.
If you want to make pure random accesses of small chunks, it won't make a big difference.
When you use low level, you can make some more optimized operations, if you take care of the file offset alignement, and size ( multiple of the needed alignement), but this not so easy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2007 05:18 PM
тАО10-09-2007 05:18 PM
Re: Difference between open and fopen
I am not looking difference in application point of view ..
I want to know how internally open and fopen (and read and fread, write and fwrite) differs.
Am sure, fopen interanlly calls open. and fopen is buffered IO, open is unbuffered IO.
open+wrapper funcionality+buffer=fopen.
I know that fopen/fread/fwrite is better than open/read/write. I want to know what benefit we are getting with fopen/fread/fwrite over open/read/write and HOW WE ARE GETTING THAT BENEFIT ?
-Masthan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2007 06:18 PM
тАО10-09-2007 06:18 PM
Re: Difference between open and fopen
This is the only thing that is important, how a particular application performs.
>I know that fopen/fread/fwrite is better than open/read/write.
Not always, it depends on your application.
>I want to know what benefit we are getting with fopen/fread/fwrite over open/read/write and HOW WE ARE GETTING THAT BENEFIT?
As mentioned several times, buffering. A simple experiment would be to have a loop write 1 byte, 1000 times. Then do this with fwrite.
And another test, use read vs fread.
You have not assigned any points yet in a year of questions. This is unmutual. Please read the following:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-10-2007 06:11 AM
тАО10-10-2007 06:11 AM
Re: Difference between open and fopen
So for instance:
a loop which fread() 64 bytes at a time 1024times, will call read() of 4k 16 times.
a loop which read() 64 bytes at a time 1024 times will call the read() syscall 1024 times.
Let's take the hypothesis that read() syscall take about 40usec without counting the time waiting for disk data. you will use aout 40msec just because you call read() 1024 times when you could have taken only 64usec.
- the time of 40usec is just an hypothesis.
So if you are making small unalign and sequential access bufferised is better.
If you do pure random, unbuffered can be better.
You can even get more perf with open/read() if your access is on a 8k boundary for a multiple of 8k bytes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-10-2007 06:41 AM
тАО10-10-2007 06:41 AM
Re: Difference between open and fopen
But all of the differences occur in user space and are, by definition, only meaningful when viewed from the standpoint of the application.
>>> I want to know how internally open and fopen (and read and fread, write and fwrite) differs.
Then I suggest that you examine the source code for Linux or OpenBSD.
>> Am sure, fopen interanlly calls open. and fopen is buffered IO, open is unbuffered IO.
Wrong again! Open() is unbuffered only with respect to user data (the application which you don't care about); it is still buffered by the UNIX buffer cache unless you specifically open() a raw character device (e.g. /dev/rdsk/c1t2d0) rather than a block device (/dev/dsk/c1t2d0) or a fully-cooked file (myfile.dat).