PDA

View Full Version : [SOLVED] [C] No explicit function to create a file?



t1497f35
October 26th, 2011, 01:02 AM
Hi,
I was wondering, there's a function to open a file "int open(...)", one to close it "int close()", to rename and to delete.
Is there a corresponding "int create" function to create a file?
Looks like not, "int mkdir" only creates dirs not files.
At this level of I/O is one supposed to use "int open (.., O_CREAT)" (and then have to close it) to create a file?

Simian Man
October 26th, 2011, 01:21 AM
Hi,
I was wondering, there's a function to open a file "int open(...)", one to close it "int close()", to rename and to delete.
Is there a corresponding "int create" function to create a file?
Looks like not, "int mkdir" only creates dirs not files.
At this level of I/O is one supposed to use "int open (.., O_CREAT)" (and then have to close it) to create a file?


If you want to create a file, presumably you want to put data in it 99% of the time. Thus you would need to use open anyway, so why have a function just to create a file?

Bachstelze
October 26th, 2011, 01:21 AM
The answer to the question is bold is no, and the answer to the other one is yes.

t1497f35
October 26th, 2011, 01:27 AM
Thanks anyone, I was wondering whether creating a file by calling "int open(.., O_CREAT)" is the proper solution, now I know it is.

Arndt
October 26th, 2011, 06:21 AM
Thanks anyone, I was wondering whether creating a file by calling "int open(.., O_CREAT)" is the proper solution, now I know it is.

There is a system call 'creat', described on the same manual page as 'open'.

Bachstelze
October 26th, 2011, 07:10 AM
There is a system call 'creat', described on the same manual page as 'open'.

It is equivalent to open(), though, and you still have to close() the fd.

t1497f35
October 26th, 2011, 08:14 PM
There is a system call 'creat', described on the same manual page as 'open'.
But it's marked as deprecated.

Arndt
October 26th, 2011, 08:55 PM
But it's marked as deprecated.

Could be, but where does it say so?

t1497f35
October 26th, 2011, 08:59 PM
The glibc docs say so, see screenshot.

Arndt
October 26th, 2011, 09:06 PM
The glibc docs say so, see screenshot.

Interesting. I wonder where they got that idea. glibc doesn't define the system calls.

Of course, since you can do the same thing with 'open', you don't need 'creat'.

To answer the original question, I would use 'fopen' followed by 'fclose', and not 'open'.

t1497f35
October 26th, 2011, 09:40 PM
Interesting. I wonder where they got that idea. glibc doesn't define the system calls.
Glibc doesn't define system calls, but the GPL states that it doesn't guarantee, nor is it responsible for anything, which makes sense since even Microsoft isn't responsible for (almost) anything despite having to pay for their products.
I also saw custom functions defined in glibc because they care more about usability rather than strict conformance. I heard in window$ there is a big pile of functions which mostly duplicate the libc functionality (I think one of the reasons is to make porting from window$ to other systems more difficult), so it's not like anyone's trying to be the most pedantic system in the world.

Bachstelze
October 26th, 2011, 10:04 PM
creat() is not going to disappear from glibc any time soon, there is too much old software that depends on it (which is the only reason why it is still in POSIX, as well).