Sunday, 13 October 2013

LINUX : Opening and Closing the File.

Posted by Unknown On 21:47 | No comments


Program to Open a File and Close it , Along with printing the File Discripter value.

Program will never return the file discripter value as 0,1,or 2 as these values are already reserved for 
0---> Standard Input
1---> Standard Output
2---> Standard Output

Here two system calls are used, read and write, you can get the details of it just by typing man read or man write in the terminal window.

Save the file in .c extension, compile by GCC compiler and execute it.

*************************
/* Program to open the File and Print its file discripter value */
/* User Progam , to be executed in the user space */
/* openfile1.c */

#include<stdio.h>
#include<fcntl.h>

int main()
{
    int fd1,fd2;
    fd1=open("test1.txt",O_CREAT|O_RDWR,0777);
    fd2=open("test2.txt",O_CREAT|O_RDWR,0777);
    if(fd1<0)
    {
        printf("File 1 cannot be open. \n");
    }
    else
    {
        printf("File Discripter value of the File 1 is : %d",fd1);
    }

    if(fd2<0)
    {
        printf("File 2 cannot be open. \n");
    }
    else
    {
        printf("File Discripter value of the File 2 is : %d",fd2);
    }
    return 0;
}


   

0 comments:

Post a Comment

Blogroll

Copyright © 2013 RAJ PRAJAPATI (raj@electronicsrj.com)

Comments

About Projects

We also Provide support in making Electronics Projects at different level at Cheapest price. Contact Us for the further help.