Tuesday, September 24, 2019

c program to head command implementation

C PROGRAM TO HEAD COMMAND


First create a file.i created a file "a.txt" and i have write 20 lines in that file.

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>     // to use read only
main(){
int n,fd,count=0;
char buffer[10];
fd=open("a.txt",O_RDONLY);
printf("enter no.of lines");
scanf("%d",&n);
while(1)
{
lseek(fd,0,SEEK_CUR);
read(fd,buffer,1);
printf("%c",buffer[0]);
if(buffer[0]=='\n'){
count++;
}
if(count==n){
break;
}
}
close(fd);
}

OUTPUT:


No comments:

Post a Comment

c program to head command implementation

C PROGRAM TO HEAD COMMAND First create a file.i created a file "a.txt" and i have write 20 lines in that file. #include&...