C++实现简单批处理文件的生成

批处理文件(百度百科)

这里用到的是一个计时关机指令:shutdown -s /t 300(这里倒计时300秒)

#include<fstream>
#include<iostream>
using namespace std;
int main()
{
	char name[]="d:\\funny.bat", something[]="shutdown -s /t 300";//设置bat文件的文件名以及储存路径和要写入的指令
	ofstream outfile;
	outfile.open(name, ios::out);//生成空的批处理文件
	if (!outfile)
	{
		cerr << "somgthing wrong !" << endl;
		abort();
	}
	outfile << something;//写入倒计时关机指令
	outfile.close();
	system("d:\\funny.bat");//system调用生成的批处理文件
}

编译运行后会在D盘根目录下生成名为funny.bat的文件,于此同时电脑提示还有300秒自动关机,若要取消倒计时自动关机,运行窗口(Windows+R)输入指令:shutdown -a,即可取消:


 
 
 

猜你喜欢

转载自blog.csdn.net/song_10/article/details/85016824
今日推荐