Completely Solved C, C++ Programs Assignment.




C program to implement copy command using command line arguments.

Filed Under:

Program Statement :
Write a C program to implement copy command using command line arguments.

Theory :
A file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characters, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image. The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file. A file is simply a machine decipherable storage media where programs and data are stored for machine usage. A text file can be a stream of characters that a computer can process sequentially. Every file being opened for any operations like :
 "r" - Read-Only mode or "r+" - read+write mode.
 “w" - Write-Only mode or "w+" - write+read mode.
 "a" - Append mode "a+" - read+append mode.
The variable argc is an argument counter that counts the number of arguments on the command line. The argv is an argument vector and represents an array of character pointers that point to the command line arguments. The size of the array will be equal to argc. For the command line given above, argc is three and argv is an array of three pointers to string.
The program uses command line arguments to implement the copy command. Three arguments are taken :
1. the name of the executable file,
2. name to the file to be copied and
3. the name of the file in which it is to be copied.
The file which is to be copied is opened in read mode i.e. in r mode and the file to which it is to be copied is opened in the write mode i.e in w mode. We are retrieving one character at a time from the file to be copied then it copies it to the output file. If more than three inputs are given as arguments, an error message is displayed.


Algorithm :
main(int argc, char*argv[])
{
FILE *source, *target;
/*If arguments are less then 3 then give an error*/
if(argc!=3)
{
print("insufficient argument");
End algorithm;
}
source = fopen(argv[1],"r"); /*open the file in read mode*/
target = fopen(argv[2],"w"); /*open the file in write mode*/
if(source==NULL OR target==NULL)
{
print ("Unable to open..ERROR in opening file");
End algorithm;
}
while((ch=fgetc(source))!=EOF)
write ch to the target file
fclose(source); /*close the source file*/
fclose(target); /*close the target file*/
}

Program listing :
/* C program to implement copy command */

#include<stdio.h>
#include<string.h>
int main(int argc,char*argv[])
{
FILE *source,*target;
char ch;
/*If arguments are less then 3 then give an error*/
if(argc!=3)
{
printf("Command Error!!Insufficient argument givenn");
return;
}
source=fopen(argv[1],"r");
target=fopen(argv[2],"w");
if(source==NULL || target==NULL)
{
printf("Unable to open..ERROR in opening file!!n");
return;
}
while((ch=fgetc(source))!=EOF)
fputc(ch,target);/*writing to the target file*/
printf("Copy is Successful.n");
fclose(source);
fclose(target);
}

Output :
[user@localhost ~]$ cat abc.txt
I am a student.
I am in 2nd year now.
I love to play computer games.
[user@localhost ~]$ ./a.out abc.txt tmp.txt
Copy is Successful.
[user@localhost ~]$ cat tmp.txt
I am a student.
I am in 2nd year now.
I love to play computer games.

Discussions :
 The argument count should not exceed 3. If it does, an error message is displayed.
 The file to which the data is to be copied should be a new file else the contents on the file would be overwritten.
 "fclose" flushes any buffers maintained for the given file and closes the file. Buffers allocated by the standard I/O system are freed.
 If the output file does not exist then it is created during runtime. If it exists then all previous contents (if any) are overwritten.



Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments