/* Hope this code is sufficient to explain everything about CL arguments */
#include
void main(int argc, char *argv[])
{
if(argc!=3)
{
printf("\n Bad Command or File name");
return;
}
else
{
FILE *fp1 = fopen(argv[1],"rb");
FILE *fp1 = fopen(argv[2],"wb");
char ch;
while(fread(&ch, sizeof(ch),1,fp1)==1)
{
fwrite(&ch, sizeof(ch),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
}
Note:
1. Save this programm as "mycopy.cpp".
2. Make exe using (F9 key), the file will be saved as "mycopy.exe".
3. copy the file ("mycopy.exe") to c: drive
4. goto command prompt (c:\>)
5 Create a text file named "text1.txt"
6. type the following: -
c:\>mycopy text1.txt d:\\text2.txt
here: -
argc = 3
argv[0] = c:\>mycopy
argv[1] = text1.txt
argv[2] = d:\\text2.txt
6.5 goto d: drive ans u will find a copy of text1.txt saved by name text2.txt
7. Now, u have ur own copy command, it can copy files of any format (such as .txt, .rtv, .exe, .bmp, etc )
2006-09-22 20:10:28
·
answer #1
·
answered by Digitally Й!Й 3
·
0⤊
0⤋
Have you ever used DOS commands? In DOS, when you need to copy a file, you give the command as follows.
C:\>COPY File.txt File2.txt
What happens is, the program called COPY.EXE will take these two command line parameters to an array. The element 1 value is File.txt and element 2 value is File2.txt. Then in side the program it’ll manipulate these 2 values. Same way, you can create a C program which accept command line parameters and manipulate them. Actually we do use command line parameters when it comes to advance programs. Not in basic programs. For example we can create a C program which will work as a calculator.
If the program name is “calc”, you can do this as,
C:\> CALC 5 + 5
So for the “calc” program you pass 3 parameters. 5, + and 5. Remember, the command line parameters are taken in string format. So you have to convert them to int before processing as numbers.
2006-09-22 19:54:30
·
answer #2
·
answered by Nishan Saliya 4
·
0⤊
0⤋
C can read "command line parameters" and use them. The purpose of a command line parameter is to pass information to the program so it knows what to do.
Like: copy A B
A and B are command line parameters for the command copy. They tell the copy command that you want to copy the file A to the file B.
2006-09-22 19:55:33
·
answer #3
·
answered by Bulk O 5
·
0⤊
0⤋
c and c++ runtimes execute a single process with an entry point provided by a function called 'main'. this entry poiint allows you to provide two arguments:
int c
and
char**
---
this allows you to pass in an array of length 'c' to parametrize your program. the return from this function can be an int or void. so you can have the following signatures:
void main ()
void main( int argc, char** argv)
int main ( int argc, char** argv)
int main()
you need to decide the following:
1) whether your program needs external input
2) whether yoru program needs to return information
2006-09-22 19:53:59
·
answer #4
·
answered by bambam 2
·
0⤊
0⤋
passing arguments from command line
u can do it while executing the files
2006-09-22 19:59:32
·
answer #5
·
answered by ch_nagarajind 3
·
0⤊
0⤋