Cat Command
This
command is used to display the contents of a text file on the terminal.
Using c programing we are now going to implement the function of copy
command.
Program
- This program should accept command line arguments, so that it behaves similar to the cat command.
- The number of arguments should be checked.
- If the number of arguments are inappropriate a message should be displayed indicating the proper syntax for usage of the command.
Code
#include<fcntl.h> //header file for file operations #include<stdio.h> #include<stdlib.h> main(int argc,char*argv[]) { if(argc!=2){ //checks if two arguments are present printf("\nThe syntax should as be follows"); printf("\nCommandname filetoread\n"); exit(1); } int fdold,count; char buffer[2048]; //characer buffer to store the bytes fdold=open(argv[1], O_RDONLY); if(fdold==-1) { printf("cannot open file"); exit(1); } while((count=read(fdold,buffer,sizeof(buffer)))>0) //displaying the content { printf("%s",buffer); } exit(0); }
C program |
Output
[student@localhost
grpb]$ gcc cat2.c
[student@localhost
grpb]$ ./a.out
The syntax
should as be follows
Commandname
filetoread
[student@localhost
grpb]$ ./a.out test
this is a
test document.
[student@localhost
grpb]$ ^C
[student@localhost
grpb]$
No comments:
Post a Comment