#include<fcntl.h>//system calls r declared in fcntl.h
int main()
{
char buff;
int count=1,fd;//at starting point count should be 1 ..so that curser may go into the while loop
fd=open("source",O_RDONLY);//O_RDONLY //O_WRONLY//O_RDWR//O_CREAT//O_TRUNC are flag used to give permissions to file
printf("\nfd=%d",fd);
while(count)
{
count=read(fd,&buff,1);//read() system call is used to read data frm source file through fd into buff untill read return 0
printf("\nread %d,%p bytes=%c",count,count,buff);//buff is printed
sleep(1);//wait for 1sec
}
close(fd);
return 0;
}
//*fd=file descriptor ...every file in user level has its own identity..when we try to open a new file ..new file will open in last available no. in fd table as per defind in usr/src/kernel/2.6.43.8-1.fc15.i686.PAE/include/linux/fd.h
//fd is a stream through which new file is opened ...
//fd=0 for stdin file
//fd=1 for stdout file
//fd=2 for stderr file
@ @ //if we close fd=1 using ....close(fd)....then we may show the output in another file which we open further after closing fd=1 & that file will open in fd=1 i.e. the least available no.at that time ...so output will go in another file instead of going on command prompt...
//As we close fd=3 & file which is not already opened ...will open at least available no.i.e.3..if file is already opened at fd=4 before closing fd=3 ...then next file will open at fd=5;;;at the moment we close file at fd=3 ;;file opened at fd=4 will not change its fd from 4 to 3 bcz it is alredy opened at fd=4;
//in same way when user close fd=1...file opened at fd=2 i.e.stderr will not change its fd from 2 to 1...its fd remain as it is ..that is...fd=2...now new file opened will be open at least available no. i.e.fd=1...when output will be displayed on that file ....