|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
DOS parameter passing
Hi everyone,
I have a quick question, which should be relatively simple....... I have built a program in VC++ V6. When I run the program from a DOS prompt, I need to be able to call it like: <programname> 100 test.txt where program name is the compiled exe file (main.exe in my case I think). eg main 100 test.txt I have set up the main function as; Code:
void main(int argc, char* argv[]) {
}
I can return the parameters in argv[], but the number is stored as a string. I need some way to either read this parameter as an integer, or be able to convert from char to int. And, at the same time, I still need the filename to be a string (so I can specify that from the command prompt too). Would anyone know of how this can be done? Many thanks...... -Andrew |
|
#2
|
||||
|
||||
|
As the programmer, you have the power and the authority to define the order of the parameters -- unless you are working from a spec. But even then, the first parameter should be defined to mean one thing, the second another, etc. And you know what those definitions are, so you know which ones to convert to integer and which ones to use as strings, etc.
To convert a string to an integer value, use atoi(). To convert a float, use atof. Or you could use sscanf if you're more comfortable with that. Code:
int int_value; int_value = atoi(argv[1]); BTW, I trust that you are aware that the first parameter, argv[0], is the name of the program. |
|
#3
|
|||
|
|||
|
Hi Dwise1.......
Unfortunately, I am working from a spec...... ![]() But thanks for the reply. I think its excactly what I need. Cheers, -Andrew |
|
#4
|
|||
|
|||
|
you should use int main() because its more proper
__________________
Roar!!! |
|
#5
|
|||
|
|||
|
G'day Seb,
Yeah, thats true. But, thruth be told, I actually "acquired" the code from somewhere else, then expanded it to my own functionality, and Ive left it "as is" in that respect. Yes, the source of the code has been correctly referenced...... BTW - Love the link......... -Andrew |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > DOS parameter passing |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|