读书人

怎么打开CMD就到指定目录

发布时间: 2013-01-21 10:15:38 作者: rapoo

如何打开CMD就到指定目录?
每次打开CMD都要手动输入路径,能不能编个控制台程序让CMD打开就到指定目录呢?

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
const char *buf="cd \\ \n e: \n cd ***\\***\\***\\";
//const char *tmp="ping 101*29.36.98";
//system("start cmd.exe");
system(buf);
system("pause");
return 0;
}

[解决办法]
该输入的还是要动手输入,
[解决办法]

//自己改改
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
char dir[100];
string cmdlines;
int wc;
cmdlines = argv[0];
wc = cmdlines.find_last_of("//");
if (wc != string::npos)
{
cmdlines = cmdlines.substr(0,wc);
}
else
{
cout <<"不能找到字符串。"<<endl;
}
int len = cmdlines.length();
cmdlines.copy(dir,len);
//sprintf(dir,"start cmd /k cd /d ",dir);
sprintf(dir,"start cmd /k cd /d ",dir);
system(dir);
system("pause");
return 0;

读书人网 >C++

热点推荐