The C++ command line argument with switches program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: Run at command line
To do: Listing the environment variables of the local computer
To show: The C++ command line and switch/option program example
// main() arguments program example of C++ codes
#include <iostream>
// For Borland 5.02 you may use <string> instead of <cstring> and comment out the using namespace std;
#include <cstring>
using namespace std;
int main(int argc, char *argv[], char *envp[])
{
// The default is no line numbers.
int LineNum = 0;
// If /n is passed to the .exe program, display numbered listing of environment variables.
// If program name and switch/option... AND stricmp...
// The _stricmp() is an ISO C++ version
if((argc == 2) && _stricmp(argv[1], "/n" ) == 0)
LineNum = 1;
else
cout<<"no \'/n\' passed..."<<endl;
// Walk through list of strings until a NULL is encountered.
for(int i = 0; envp[i] != NULL; ++i)
{
if(LineNum)
cout<<i<<": "<<envp[i]<<"\n";
}
cout<<"Usage: searchpattern /n\n";
return 0;
}
Output example:
(This program run at the command prompt)
F:\vc2005project\searchpattern\debug>searchpattern
no '/n' passed...
Usage: searchpattern /n
F:\vc2005project\searchpattern\debug>searchpattern /n
0: ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS
1: APPDATA=C:\Documents and Settings\Johnny\Application Data
2: CLIENTNAME=Console
3: CommonProgramFiles=C:\Program Files\Common Files
4: COMPUTERNAME=MYPERSONAL
5: ComSpec=C:\WINDOWS\system32\cmd.exe
6: FP_NO_HOST_CHECK=NO
7: HOMEDRIVE=C:
8: HOMEPATH=\Documents and Settings\Johnny
9: INCLUDE=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
10: LIB=c:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\
11: LOGONSERVER=\\MYPERSONAL
12: NUMBER_OF_PROCESSORS=2
13: OS=Windows_NT
14: Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\Microsoft SQL Server\90\Tools\binn\
15: PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
16: PROCESSOR_ARCHITECTURE=x86
17: PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 4, GenuineIntel
18: PROCESSOR_LEVEL=15
19: PROCESSOR_REVISION=0304
20: ProgramFiles=C:\Program Files
21: PROMPT=$P$G
22: SESSIONNAME=Console
23: SystemDrive=C:
24: SystemRoot=C:\WINDOWS
25: TEMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp
26: TMP=C:\DOCUME~1\Johnny\LOCALS~1\Temp
27: USERDOMAIN=MYPERSONAL
28: USERNAME=Johnny
29: USERPROFILE=C:\Documents and Settings\Johnny
30: VS71COMNTOOLS=c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\
31: VS80COMNTOOLS=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\
32: windir=C:\WINDOWS
Usage: searchpattern /n
F:\vc2005project\searchpattern\debug>