i am trying to pass a filename to my program from a promp but i don't know how to pass a file handling argument to main so that main can open the file and so on. any advices? thanks.
dj.
i am trying to pass a filename to my program from a promp but i don't know how to pass a file handling argument to main so that main can open the file and so on. any advices? thanks.
dj.
Please post your code!
Show what you are doing...
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
explainationCode:int main(int argc,char* argv[]) { char buf[20]; FILE* fp = fopen(argv[1],"r"); if(fp == NULL) return -1; fread(buf,1,20,fp); printf("%s\n",buf); fclose(fp); return 0; }
int main(int argc,char* argv)
this declaration of main tells it to take command line arguments
where argc is the number of arguements;
and argv contains the actual arguements
Note: argv[0] always contains the programs name sothat would be the name of the exe
Last edited by no-one; 09-05-2001 at 08:33 PM.
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
no one's answer contained a typo... main should look like this:
int main (int argc, char * argv[])
or
int main (int argc, char** argv)
mxr
THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.
thanks mix0,
that would have caused some trouble i'll edit and fix it.
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
fyi: no-one's code is actually right. in this case char **argv and char *argv[] accomplish the same thing. char* argv[] just means an array of character pointers, each pointer being the base address of a parameter string. either way should work with the compiler.
actually i edited it to * argv[] so it would not confuse him. it was when mix0 posted my code was * argv so his correction was quite right.
Last edited by no-one; 09-06-2001 at 11:28 AM.
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
int main(int argc, char *argv[])
you can use the single star as previously stated all this is doing is point to an unbound array that stores command line inputs, I've always used the single star and not had any problems
just remember that unlike most arrays argv[] will start at 1 to what ever, this is that the actual program itsself is the argv[0] part.
so if you entered something like
compare txt1 txt2 txt3
argv[0] = compare
argv[1] = txt1
argv[2] = txt2
argv[3] = txt3
if you are uncertain write a small program that displays the content of the command line input
hope this helpsCode:int main(int argc, char *argv[]) { printf("You enetered the following command prompt %s %s %s %s", argv[0], argv[1], argv[2], argv[3]); }
-ali