Thursday 29 September 2011


IMPLEMENTATION OF I/O SYSTEM CALLS
[open(),read(),write()]

AIM:
To write a c program to perform operations on files using LINUX system calls open(),read(),write()

ALGORITHM:

Step1: Start the process.
Step2: Declare and initialize an array variable.
Step3: Declare a file pointer.
Step4: Using the open() system call, open a file in a write mode and store it in a file
Pointer.
Step5: Using the write()system call, write the contents of an array to the file.
Step6: The written file is read with the help of read() system call. Then Close the file.
Step7: Stop the process.

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
double d[10]={1.3,1.0,1.23,1.9,0.91,1.5,1.4,0.0,1.01,8.15};
int main( void)
{
int I;
FILE *fp1;
if((fp=fopen(“my.txt”,”wb”))==NULL)
{
printf(“Cannot open file \n”);
exit(1);
}
/* write the entire array in one step */
if(fwrite(d,sizeof(d),1,fp)!=1)
{
printf(“write error \n”);
exit(1);
}
fclose(fp);
if((fp=fopen(“my.txt”,”rb”))==NULL)
{
printf(“Cannot open file \n”);
exit(1);
}
/*clear the array */
for(i=0;i<10;i++)
d[i]=0.0;
/* read the entire array in one step */
if(fread(d,sizeof(d),1,fp)!=1)
{
printf(“read error \n”);
exit(1);
}
fclose(fp);
/*display the array */
for(i=0;i<10;i++)
printf(“%f”,d[i]);
return 0;
}

OUTPUT:
1.300001.1.000001.230001.90000000.9100001.5000001.40000000.000001.0100008.150000

RESULT:
Thus the given C Program is executed and output is verified.

0 comments:

Post a Comment