Saturday, January 15, 2011

OS-> I/O SYSTEM CALL

I/O SYSTEM CALL

AIM:

          To write a c program using i/o system call to open, read and write a file.

ALGORITHM:

        STEP 1: start the program.
        STEP 2: include the header file as<fcntl.h> and <sys/stat.h>
        STEP 3: create the buffer variable with size of 50.
        STEP 4: create a new file using cat and write the data into the file.
        STEP 5: open a file in read only mode and read the content of file and place                                                                                                                 
                       it in buffer.    
        STEP 6: write the buffer content to a file which is opened in write
                       only mode.
        STEP 7: close the files.
        STEP 8: stop the program.




















PROGRAM:

 #include<stdio.h>
 #include<fcntl.h>
 #include<sys/stat.h>
 #include<sys/type.h>
 int main()
  {
   int fp1,fp2,n;
   char buff[50];
   fp1=open("a.txt",O_RDONLY);
   fp2=open(“b.txt”,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);  
   while(n=read(fp1,buff,50)>0)
   write(fp2,&buff,50);
   close(fp1);
   close(fp2);
   exit(0);
  }





OUTPUT:

  $ cc ios.c
  $./a.out
  $ cat b.txt
  hai
  hello