C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 16th, 2003, 05:53 AM
thealfheim thealfheim is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Australia
Posts: 13 thealfheim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to thealfheim
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

Reply With Quote
  #2  
Old June 16th, 2003, 08:54 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 29 m 15 sec
Reputation Power: 462
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.

Reply With Quote
  #3  
Old June 16th, 2003, 10:59 AM
thealfheim thealfheim is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Australia
Posts: 13 thealfheim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to thealfheim
Hi Dwise1.......

Unfortunately, I am working from a spec......

But thanks for the reply. I think its excactly what I need.

Cheers,
-Andrew

Reply With Quote
  #4  
Old June 16th, 2003, 08:52 PM
RoaringSeb RoaringSeb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 52 RoaringSeb User rank is Private First Class (20 - 50 Reputation Level)RoaringSeb User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 40 m 50 sec
Reputation Power: 6

you should use int main() because its more proper
__________________
Roar!!!

Reply With Quote
  #5  
Old June 16th, 2003, 11:16 PM
thealfheim thealfheim is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Australia
Posts: 13 thealfheim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to thealfheim
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > DOS parameter passing


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT