获取当前执行程序的名称

作者在 2008-06-08 17:17:43 发布以下内容
考虑到main函数第一个参数就是程序的名称,因此很容易写出下面的代码:
#include<iostream>
int main(int argc, char** argv)
{
    std::cout<&lt;argv[0]&lt;&lt;std::endl;
}
别慌,输出来看看,怎么这个不光是程序的名称,还包含路径呀!真是累赘
那下一步当然是删除这个累赘了
使用下面的代码:
#include &lt;iostream.h>
#include <string.h>

int main(int argc, char** argv)
{
    cout&lt;&lt;argv[0]&lt;&lt;endl;
    char *p = strtok(argv[0], "\\"),*name=NULL;
    while ( (NULL != p)&&(name=p) ){
        cout&lt;&lt;p&lt;&lt;endl;
        p = strtok(NULL, "\\");
    }
    cout&lt;&lt;name&lt;&lt;endl;
    return 0;
}
简单吧,是不是你也会了
技术 | 阅读 5537 次
文章评论,共2条
vfdff(作者)
2008-06-08 17:22
1
另外,如果使用的是MFC工程,可以使用下面的函数获取程序名称:<br />
CWnd* pWnd;<br />
// Set pWnd to some CWnd object whose window has already been created.<br />
<br />
// The following call to CWnd::MessageBox uses the application<br />
// title as the message box caption.<br />
pWnd-&gt;MessageBox(&quot;Some message&quot;, AfxGetApp()-&gt;m_pszAppName);<br />
<br />
// A more direct way to get the application title is to <br />
// call AfxGetAppName:<br />
pWnd-&gt;MessageBox(&quot;Some message&quot;, AfxGetAppName());<br />
<br />
// An easier way to display a message box using the application<br />
// title as the message box caption is to call AfxMessageBox:<br />
AfxMessageBox(&quot;Some message&quot;);
lone
2008-06-12 20:57
2
看不懂的!郁闷!
游客请输入验证码
浏览1941166次