"main" is the entry point for a c or c++ program. usually we declare it like this
void main( void ) or void main( )
the above version of main takes 0 arguments. there is another variation of main that takes 2 arguments...
int main( int argc, char *pArgv[] )
whoa what is this? the int argc is how many arguments were passed to main,
and the pArgv is a pointer to the array of arguments. When main is declared in
this format, and the user doubleclicks the exe - here it was happens:
Windows passes 1 arguement to main, the name of the program. In this case
argc == 1
and pArgv[0] == "myProgram.exe"
neat I suppose, but not too useful. However, if a user drags a file
over the program, then the second argument is the name and path of the dragged file.
Lets say the user drags "notes.txt" over myProgram.exe
then
argc == 2
pArgv[0] == "myProgram.exe"
pArgv[1] == "c:\notes.txt"
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(int argc,char *pArgv[])
{
printf( "\n Value received by argc is %d.\n", argc );
printf( "\n There are %d command line arguments\n", argc );
printf( " passed to main().\n" );
printf( "\n The first command line argument is:\n" );
printf( " %s\n", pArgv[ 0 ] );
if ( argc == 2 )
{
printf( "\n The second command line argument is:\n" );
printf( " %s\n", pArgv[ 1 ] );
}
else
{
printf( "Please drag your file across the exe. " );
}
getch( );
return 0;
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <string.h>
#include <stdio.h>
int main(int argc,char *pArgv[])
{
char sourceName[260];
char destName[260];
unsigned char byte;
if (argc==2)
{
//This takes the filename of the file that
//you drag onto the exe and copies the name
//to sourceName
strcpy( sourceName, pArgv[1] );
//when you drag your file over the .exe, this
//program will create a new file and copy
//the original file and any data in it to
//the same directory as the original.
//we can't use the same name so we will take the
//original text document name and add .duplicate.txt
//to the end. Not pretty but good enough for now
sprintf( destName, "%s.duplicate", sourceName );
//open the source file for reading and the destination
//file for writing.
FILE *pSource = fopen( sourceName, "rb" );
FILE *pDest = fopen( destName, "ab" );
//read one byte at a time from the source file
//and place it at the address of "byte"
//Will read until the end of the file "!= 0"
while ( fread( &byte, sizeof(unsigned char), 1, pSource ) != 0 )
{
fwrite( &byte, sizeof(unsigned char), 1, pDest );
}
//close the open files
fclose( pSource );
fclose( pDest );
}
else
{
//if you double click the exe it will print this
printf("\n\n Please drag and drop your file on the exe.\n\n ");
printf("Press enter to exit this screen. ");
}
return 0;
}