All about programming in GNU/LINUX

Program to create a simple file in linux , using linux system call

//here open is not a standard C Call , its a linux system call
//file operations can be performed using standard calls or using linux system calls
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
int main()
{
    int n;
    n=open(“/home/karthic/Desktop/test.txt”,O_CREAT,S_IRUSR);//replace this with a valid path on your computer
    /* 1)Open function call returns a integer called as FileDescriptor , which will be associated with the file whose path is
         is mentioned as the first argument to the open function call
        2)This return value from open  function call , that is the returned file descriptor will be used to perform read ,write operations on the file
        3)The second parameter specifies that we are just creating  a file and the 3rd parameter specifies the permission for the file created
        4)the format of the third parameter is as follows
           *S_IRUSR
           *S_IWGRP
           *S_IXOTH
           As you can see S_I is common amongst all the options , then it must be followed by either ‘R’ or ‘W’ or ‘X’ , which stands for Read , write and
           execute respectively.
           Then it must be followed by either USR,GRP and OTH to apply the permissions either to Users , groups or other users on the system*/
    printf(“\nThe file descriptor associated with the file is :%d “,n);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s