Helpful Information
 
 
Category: Computer Programming
C File Processing

Hi,

I'm making a program that will read information from a file, and when exited, will write to a new file. These files are to be inputted at the time of typing the executable file.
eg <executable file> <file.inp> <file.out>

Now, i know how to read from/print to the files using fopen/close, fgets/puts, sscanf/sprintf, but what i dont know is how to actually let the program know that the words typed after the executable are the names of the 2 files.

Does it have to do with putting declarations inside the main brackets?
eg
int main( xxx, yyy)


I tried this but kept getting errors as it says the first and second declarations need to be an int and **.
Any help would be very much appreciated.

Thanks.

dole_4,

Yes the parameters in the main function have the parameters supplied after the command.
int main( int argc, char* argv[])The argc parameter details the number of parameters given on the command line. This is always at least 1 as the name of the program, as typed by the user, is the first parameter. The argv parameter is an array of character strings on the command line. The following program demonstrates how to access the parameters.
int main(int argc, char* argv[])
{
int i = 0;
printf("Numb of parameters: %i\n", argc );
for( i = 0; i < argc; i++ ) printf( "Par %i: %s\n", i, argv[i]);
return 0;
}Hope this helps.

it is done with argument count (int argc) and argument vamue (char *argv[])
argv[0] contains the name of your exe. this is argumant 0

here is an example program:


#include <stdio.h>

int main(int argc, char *argv[])
{
printf(" the arguments are: ");
for(int i=0;i<argc;i++)
{
printf("\n%s\n",argv[i]);
}
return 0;

}

damn too late

fivesidecube, next time, type slower. :D

yep that helps ...

thanks alot










privacy (GDPR)