Command line Argument to be parsed as integer value.

I have to parse the command enter in the command line argument and convert certain parameter of them into integer variable.

I can parse them but cannot convert them into integer . I have to convert the digits after '+' sign to integer variable.

The error is displayed as "Debug Assertion Error".

I am using Win32 API and code is as below:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{

	TCHAR *cmd_line = GetCommandLine(); 
	TCHAR *token1;
	token1 = strchr(cmd_line, L'+');
	MessageBox(NULL, token1, _T("test"), NULL);
	
        int i, value=0;
	for ( i = 0; token1[ i ] != '\0'; ++i )
        {
           int digit = token1 [ i ] - '0';// get value of current digit character 
           value = 10 * value + digit;
        }


It can build sucessfully but when i run it , it shows "Debug Assertion Error"
Last edited on
i havent used the winapi too much before, but there might be a way to use atoi with it or the c way which is: - '0'. you could go the c++ route and use <stringstream>
Did you try tracing the error with your debugger? Tell us the exact line this happens.
The detail in the popup error is

Debug Assertion Failed!

Program: ......WesOPC.exe
File:f:\dd\vctools\crt_bld\self_x86\crt=src\tcscpy_s.inl
line:19

Expression : (((_Src)))!=NULL

When i did debugging operation , the error is seen in token1[i] expression, in line "for ( i= 0; token1 [ i ] != '\0'; ++i)"

The error is

Unhandled exception at 0x0042847f in MinOPC.exe: 0xC0000005: Access violation reading location 0x00000000.

Last edited on
It looks like your token1 pointer may be null. Try using the debugger again and checking the values of your variables as you step through the program.
strchr returns a NULL pointer on failure, which is the case here.
If you are compiling in Visual Studio, the above code is likely mixing types. TCHAR is defined as WCHAR if UNICODE or _UNICODE is defined, which are usually defined by default in visual studio projects. So your cmd_line variable is likely of type WCHAR *. You can't pass a WCHAR * to strchr which expects a CHAR *.
Topic archived. No new replies allowed.