Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++: Parsing command line args
New Posts  All Forums:Forum Nav:

C++: Parsing command line args

post #1 of 6
Thread Starter 
Hi,

I will have a command line execution as follows:

./progname hello 50

I need to be able to get "hello" as a cstring and 50 as an int. I've been having a LOT of trouble with this, as it's my first time getting arguments from the command line.

Can someone help me? I'm guessing it should be relatively simple overall, but I'm having trouble copying characters from argv[] since it's type char** or something.
Current
(12 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Z68 Extreme4 Gen3 EVGA GTX 570 Classified 4x4GB Corsair XMS3 
Hard DriveCoolingPowerCase
Samsung 830 Thermaltake Frio OCZ ZS 850W Lian Li Lancool First Knight K9 
MouseAudioOtherOther
Logitech G500 Audinst HUD-MX1 Sennheiser HD598 Swan D1080MkII 
  hide details  
Reply
Current
(12 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Z68 Extreme4 Gen3 EVGA GTX 570 Classified 4x4GB Corsair XMS3 
Hard DriveCoolingPowerCase
Samsung 830 Thermaltake Frio OCZ ZS 850W Lian Li Lancool First Knight K9 
MouseAudioOtherOther
Logitech G500 Audinst HUD-MX1 Sennheiser HD598 Swan D1080MkII 
  hide details  
Reply
post #2 of 6
probably not the best or easiest way but it works
Code:
#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {
   string str = argv[1];
   char * cstr;
   cstr = new char [str.size()+1];
   strcpy (cstr, str.c_str());
   int num = atoi(argv[2]);
   cout<<"str: "<<str<<"; num: "<<num<<endl;
   return 0;
}
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 6
Thread Starter 
I haven't tried it, but I don't see how that could work.

str would get "/progname hello 50"
and
argv[2] would get "progname hello 50", meaning num would get 0.

Edit: Maybe I'm misunderstanding what argv[] actually is...I'll try this when I get a chance.

Thanks!
Edited by flushentitypacket - 4/29/11 at 7:53am
Current
(12 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Z68 Extreme4 Gen3 EVGA GTX 570 Classified 4x4GB Corsair XMS3 
Hard DriveCoolingPowerCase
Samsung 830 Thermaltake Frio OCZ ZS 850W Lian Li Lancool First Knight K9 
MouseAudioOtherOther
Logitech G500 Audinst HUD-MX1 Sennheiser HD598 Swan D1080MkII 
  hide details  
Reply
Current
(12 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k ASRock Z68 Extreme4 Gen3 EVGA GTX 570 Classified 4x4GB Corsair XMS3 
Hard DriveCoolingPowerCase
Samsung 830 Thermaltake Frio OCZ ZS 850W Lian Li Lancool First Knight K9 
MouseAudioOtherOther
Logitech G500 Audinst HUD-MX1 Sennheiser HD598 Swan D1080MkII 
  hide details  
Reply
post #4 of 6
argv[] is a array filled with all of the arguments passed to your program (the first one is always your program, so skip it).

argc is the number of arguments passed.
post #5 of 6
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #6 of 6
http://www.boost.org/doc/libs/1_39_0...m_options.html
This is what you really want ^

Boost has a library specifically for this.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++: Parsing command line args