/*A NICE PROGRAM TO ENTER THE COMMENT WHICH IS SAVED INTO SOME FILE & READ THAT COMMMENT*/
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int count,wfd,fd;
char arr[100],usr[4];
//memset(,'\0',sizeof());
wfd=open("./anu",O_WRONLY|O_CREAT|O_TRUNC);//open THE FILE INTO WHICH COMMENT IS TO BE SAVED..IN O_WRONLY MODE..& CLEAR ALL THE DATA INTO FILE BY USING O_TRUNC...SO THAT COMMENT SHOULD NOT BE OVERRITTEN ...NEW COMMENT SHOULD BE RECEIVEDBY REMOVING PREVIOUS COMMMENT..//
printf("ENTER THE COMMENT:\n");
scanf("%[^\n]",arr);//ENTER THE CHARACTERS OF COMMMENT USING SCANF...^\n will MAKE ABLE TO SCANF TO LEAVE THE SPACE BETWEEN THE CHARACTERS...//
write(wfd,arr,strlen(arr));//WRITE ALL CHARACTERS OF ARRAY INTO FILE ...WHERE THAT COMMENT IS SAVED//
printf("DO U WANT TO READ,,ENTER THE COMMAND:\n");
scanf("%s",usr);//ENTER read ...if u want to read the COMMENT...//
if(strncmp(usr,"read",4)==0)//COMPARE IF USR HAD ENTER READ COMMAND...//
{
fd=open("./anu",O_RDONLY);//OPEN same file anu in O_RDONLY MODE//
printf("fd=%d\n",fd);
lseek(fd,0,SEEK_SET);//SET POSITION OF CURSER AS 0..//
memset(arr,'\0',sizeof(arr));//MEMSET ARRAY BY 0
count=read(fd,arr,sizeof(arr));//READ ALL THE CHARACTERS FROM fd TO arr
printf("count=%d\n",count);
printf("%s",arr);//print that value which is into that array...//
[root@localhost]/home/c-asignments/assign4# ./a.out ENTER THE COMMENT: HELLO I HAD MADE PROGRAM BASED ON WRITING A COMMENT INTO FILE DO U WANT TO READ,,ENTER THE COMMAND: read fd=4 count=62 HELLO I HAD MADE PROGRAM BASED ON WRITING A COMMENT INTO FILE # [root@localhost]/home/c-asignments/assign4# vim anu
THIS IS ANU NAMED FILE:----
HELLO I HAD MADE PROGRAM BASED ON WRITING A COMMENT INTO FILE