+ Reply to Thread
Results 1 to 2 of 2

Thread: How to parse command line arguments in MFC?

  1. #1

    How to parse command line arguments in MFC?

    How to parse command line arguments in MFC?

    Provide some example.

  2. #2
    Following is the example:

    Code:
    class CParseCommandLineInfo : public CCommandLineInfo
    {
    private:
    	enum CommandTypes
    	{
    		eNone		= -1 ,
    		eInputFile	= 0 ,
    		eOutputFile = 1 ,
    	} ;
    	CommandTypes m_nNextCommandType ;
    	CString m_strInputFileName ;
    	CString m_strOutputFileName ;
    
    public:
    	CParseCommandLineInfo()
    	{
    		m_nNextCommandType = eNone ;
    		m_strFileName	= "" ;
    	}
    
    	virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
    	{
    		if (m_nNextCommandType == eNone)
    		{
    			if (pszParam[0] == 'i')
    			{
    				m_nNextCommandType = eInputFile ;
    			}
    			else if (pszParam[0] == 'o')
    			{
    				m_nNextCommandType = eOutputFile ;
    			}
    		}
    		else
    		{
    			switch(m_nNextCommandType)
    			{
    			case eInputFile:
    				{
    					m_strInputFileName = pszParam ;
    				}
    				break ;
    			case eOutputFile:
    				{
    					m_strOutputFileName = pszParam ;
    				}
    				break ;
    			}
    
    			m_nNextCommandType = eNone ;
    		}
    	}
    } ;
    
    BOOL CMyApp::InitInstance()
    {
    	CParseCommandLineInfo objParseCommandLineInfo ;
    	ParseCommandLine(objParseCommandLineInfo) ;
    }
    
    //Run application using following commands:
    //-i "C:\File1.txt" -o "C:\File2.txt"
    Regards.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts