Linux Program to Create a File using File Descriptors

This Linux Program illustrates Creating a new File using File Descriptors. To create a new file, we will going to use
creat() system call that takes two arguments. The first argument is the file-name and the second one is mode which sets the permissions of read, write and execute for file owner. In this example, we are using the mode as S_IRWXU which indicates that the file owner has the permissions to read, write and execute the file.

Here is the source code of Linux Program to Create a File using File Descriptors. The Linux Program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * Linux Program to Create a File using File Descriptors.
  3.  */
  5. #include 
  6. #include 
  7. #include 
  8. #include 
  9. #include 
  10. #include 
  12. int main(int argc, char **argv)
  13. {
  14.     int fd;
  16.     if (argc  0) { 
  17.         printf("New file %s created!\n", argv[1]);
  18.     } else {
  19.         perror("Problem in creating the file.");
  20.         exit(1);
  21.     }
  22. }

$ gcc create_file.c
$ ./a.out new_file
After executing the above command, creat() system call tries to create 
a new file with the name new_file and if it has been created successfully 
then we will see the output given below,
New file new_file created!

Sanfoundry Global Education & Learning Series – 1000 Linux Programs.

If you wish to look at all Linux Programming examples, go to Linux Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.