Problem in opening a file!

Hello All,
I am new to programming in unix.
I am trying to create a file by using open command. The entire file is :

#include<fcntl.h>
main(void){
int fd;
fd = open("File1", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRWXO);
printf("%d ", fd);
}

The problem I am facing a strange problem. When I execute the above file (using gcc), I dont see File1 getting created! But if I change the filename with any other name (say File2), I am able to get create the file!

Could anyone pls tell me what could be happening?

Regards,
karthik

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Dear Friend,

Your code in correct. But you need to include the necessary header files.

I ran your code. It is not working. It showed me some error. After that I include the some header files. Then your code is working fine.

#include<stdio.h>
#include<fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

main(void){
        int fd;
        fd = open("File1", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRWXO);
        printf("%d ", fd);
}

You need to include above two header files.