CPP .EXE file Disappears on Windows

Simple code. it creates a batch file that shuts down machine on a timer using windows command prompt command shutdown -s -t xxx. the xxx is a variable named sec. I used cout and cin to ask for minutes variable name mins. I multiplied by 60 to get seconds. I closed the batch file. I used system to open the file and commence the shutdown process. Everything works perfect!

The problem is that the exe and the batch file disappear completely after the first execution of the code. Not by running in IDE, but by actually clicking the EXE. It works but then its gone. I have been all day working on tryna fix it. I know its stupid simple and believe its windows defender.

I have tried on 2 machines, win 10 and 8.1.

So frustrated at myself for not figuring this out seems so simple lol.

Code is below.
THANKS IN ADVANCE TO EVERYONE!!



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

int main(){
ofstream OutputFile;
int Mins;
int Sec;


cout<<"Enter in how minutes you would like your computer to remain powered on and press enter... \n";
cin>>Mins;
Sec = Mins * 60;
OutputFile.open("ShutEye.bat");
OutputFile<<"shutdown -s -t "<<Sec;
OutputFile.close();

system ("ShutEye.bat");

return 0;

}
Last edited on
Didn't ran your program, but a piece of advice I want to give you is to don't use the system command. For windows, use CreateProcess() instead.

http://www.cplusplus.com/articles/j3wTURfi/
Last edited on
Worked "as intended" on my machine (Windows 7; no Windows defender). The relevant exe and bat files are still there after the reboot.

I added #include <cstdlib> for the system command, but that won't be your issue.

Could be quite a useful tool for bringing a halt to over-running meetings!
Yea i Have a win 7 machine. I need to try it on It prob will work on there. I made the program for when people want to fall asleep binge watching netflix series but dont want pc on all night. I actually made it for my wife lol. Anyways, Thanks alot for your responses and I will try the CreateProcess() instead. hopefully this will work. I'll kepp you guys posted. Thanks again!
An alternative would be to use the ExitWindowsEx function.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx
Thank you all for your responses! I have finally figured it out and learned a little more from each of you guys recommendations. The program works perfectly and may come in handy if you want a copy. If you copy this code and compile it save the exe in a folder place that folder anywhere. the batch file will be created to that folder. create a shortcut of the exe and put it on your desktop. bam! now you have a shutdown timer! I call it Shut Eye.I made it fore my wife who binge watches netfilx and leaves her computer on all night. lol

anyways here is the code!

#include<iostream>
#include<fstream>
#include <windows.h>
using namespace std;

void StartProgram (string File)
{
ShellExecute(NULL,"open",File.c_str(),NULL,NULL,SW_SHOWNORMAL);
}

int main(){
ofstream OutputFile;
int Mins;
int Sec;

cout<<"Enter in how minutes you would like your computer to remain powered on and press enter... \n";
cin>>Mins;
Sec = Mins * 60;
OutputFile.open("ShutEye.bat");
OutputFile<<"shutdown -s -t "<<Sec;

OutputFile.close();
StartProgram ("ShutEye.bat");

}
ok, its neat to learn how to do this but all you needed was a 1-2 line batch file. Now see if you can do it that way ... batch files are a great thing to know, and can be used with your programs, inside them, alongside them, etc to do more stuff with less work. Its even more potent if you put something like cygwin on your PC and hack the path so that from a dos prompt you can call the linux commands...


Last edited on
Topic archived. No new replies allowed.