Sponsored Content
Full Discussion: fopen and open
Top Forums Programming fopen and open Post 61588 by Perderabo on Saturday 5th of February 2005 11:24:52 AM
Old 02-05-2005
It's "creat", not "create". Originally, the open system call could only open files that existed. Now that you can do "open(file, O_CREAT|O_TRUNC|O_WRONLY, mode)", creat no longer has any great use. But lots of code had been written using creat, so it persists. Don't use creat, it's day is long past.

The system calls like read, write, and open must exist if programs are going to be able to use the kernel's drivers. Stuff like fopen, fclose, fread, etc are put of the standard I/O library or stdio. stdio includes other routines like getchar, putchar, etc. So not all of the names start with f. stdio was developed as part of the C language. When C is implemented on other platforms, the stdio library will be there (the ansi C standard mandates this). This is not necessarily true of the unix system calls. So to write C code truely portable to non-unix systems, you must use stdio.

If your program is intended for unix systems only, the most important factor is i/o buffering. If you want to read a file character by character, issuing separate read system calls is slow. A system call is expensive. Even with a disk file, where the data is being fetched from the buffer cache, a separate read for each character will take too long. By using stdio, one read will happen to load some data into the library's buffer. Now if you read character by character, it will go much faster. And that's with a disk file which is using a buffer cache. If you are doing i/o to, say, /dev/tty, there is no buffer cache. Each read and write goes all the way to the driver. On the other hand, if you are reading a disk file block by block, the stdio buffering will probably slow you down. The data is read from the disk into the buffer cache. Then it's copied from the buffer cache into stdio's buffer. Then it's copied from stdio's buffer into your program. So for each application, you need to consider which technique is faster.

Another consideration is that stdio has a richer feature set than is available directly from the system call interface. You can do an ungetc() but there is no unread(). And printf has a ton of features that write() can't do.
 

10 More Discussions You Might Find Interesting

1. Programming

Mac OS X - open() and fopen() with French filename

Hi I was trying to open a file with french name on Mac OS-X with open() and fopen() but it didn't work.Do we have any POSIX unix func. which can be used to open any file with special name. if anybody has an idea plz help. Thanks Mohit (1 Reply)
Discussion started by: mohit grover
1 Replies

2. Programming

.cc fopen failed - Broken Pipe

hello.. i make some code with C in freebsd 5.4 and compile it in solaris somehow i succeed compile the program. but when i run it, i got error message "Broken Pipe" i looked out the syntax that that caused this, fp = fopen("file.tmp","r"); does anyone know why, and how to solve this... (3 Replies)
Discussion started by: kuampang
3 Replies

3. Programming

fopen() + reading in large text files

For reading in large text files (say files over 1kB in size) are there any issues with fopen() that I should be aware of ? cheers (2 Replies)
Discussion started by: JamesGoh
2 Replies

4. Web Development

CAN TCPDF USE fopen() or Convert URL To PDF?

Dear all, I'm a newbie for PHP and TCPDF ,I have to change the URL to PDF, so I used FPDF , But it cannot convert most of the advanced HTML tags. So explored again and found TCPDF , it can do most of the tag but I cannot found to change URL to PDF. So Does anyone can point the example... (0 Replies)
Discussion started by: athae
0 Replies

5. UNIX for Advanced & Expert Users

Linux fopen() mistery. Help required.

Hello! I'm having problems with fopen() call in Linux. I have shared library (created by myself) that implements some file operations: int lib_func(char* file_name) { ... fd = fopen(file_name, "r"); if(!fd) {... exit with error ...} ... do something useful using fd ... ... (2 Replies)
Discussion started by: kalbi
2 Replies

6. Programming

fopen() - don't know what I'm doing wrong

This code works fine when I use a command line argument for fopen()'s parameter, but when I change it to a filename, the program freezes upon compilation. input.txt is definitely there, so I can't figure it out. Thanks. #include <stdlib.h> #include <stdio.h> #include <ctype.h> int... (3 Replies)
Discussion started by: lazypeterson
3 Replies

7. Programming

overhead of fopen/freopen

I always assumed the fopen/freopen is very costly, so when I needed to work with many files within on process I spent extra time to implement a list of FILE * pointers to avoid extra open/reopen but it did not produced any better results. Here is a task at hand - there is a huge stream of data... (4 Replies)
Discussion started by: migurus
4 Replies

8. Programming

Segmentation fault in fopen when in write mode.

Hi All, Although this code is quite long but it does not require one to understand the logic of the code. I am trying to run this code on my Linux machine using gcc. But when this line is executed: output_pointer = fopen ( file_name , "w" );I get segmentation fault. I've been breaking my head... (17 Replies)
Discussion started by: shoaibjameel123
17 Replies

9. Programming

help plz - fopen()

Hello, I have a problem here, I want to write a function called"myfopen()" instead of "fopen()" for writing this function I must not use the <stdio.h> library, Can you help me? thanks a lot (2 Replies)
Discussion started by: hamed.samie
2 Replies

10. Shell Programming and Scripting

Mac. PHP fopen() does not create a file. Permissions.

5Thank you to those who responded. After a crazy amount of troubleshooting and getting hints and feedback from others, I was so darn determined to get on with my tutorials and I found the solution myself. Keyword search: php and 'Mac computer' and fopen and chmod. Using:php and Mac and... (1 Reply)
Discussion started by: iHaveAQuestion
1 Replies
fread(3s)																 fread(3s)

Name
       fread, fwrite - buffered binary input/output

Syntax
       #include <stdio.h>

       size_t fread(ptr, size, nitems, stream)
       void *ptr;
       size_t size, nitems;
       FILE *stream;

       size_t fwrite(ptr, size, nitems, stream)
       void *ptr;
       size_t size, nitems;
       FILE *stream;

Description
       The  function  reads  into a block beginning at ptr, nitems of data of the size size (usually sizeof *ptr) from the named input stream.	It
       returns the number of items actually read.

       If stream is stdin and the standard output is line buffered, then any partial output line will be flushed before any call to to satisfy the

       The function appends, at most, nitems of data of the size size (usually sizeof *ptr) beginning at ptr  to  the  named  output  stream.	It
       returns the number of items actually written.

Return Values
       The and functions return 0 upon end of file or error.

See Also
       read(2), write(2), fopen(3s), getc(3s), gets(3s), printf(3s), putc(3s), puts(3s), scanf(3s)

																	 fread(3s)
All times are GMT -4. The time now is 05:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy