Pause execution until a certain time in C

The new programmer is here. Like super new. I am trying to write a program for my father.

#include <stdio.h> #include <time.h> int main() { int CurrentTime; system("start https://www.youtube.com/watch?v=FchMuPQOBwA"); return 0; } 

While I have it. How can I do this so that he cannot open it before his birthday or at a specific time? I looked on time .h and googled a little, but I can not find a solution. Also, how do I send it to him, so it's just a .exe and he doesn’t see the code?

Thank you in advance

+6
source share
2 answers

There is an example of all the functions that you will need to use the "time ()" function in a link. http://www.cplusplus.com/reference/ctime/time/

Actions to be performed:

  • Get current time (c_time)
  • Get your father's birthday
  • (b_time) Check if c_time is greater than b_time.

Here is an example program:

 #include <stdio.h> /* printf */ #include <time.h> /* time_t, struct tm, difftime, time, mktime */ int main () { time_t c_time, b_time; struct tm b_date; double seconds; y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; //January first, 2000. I'll let you change that because I don't know when the big day. time(&c_time); /* get current time; same as: timer = time(NULL) */ b_time = mktime(&b_date); seconds = difftime(c_time,b_time); if(seconds < 0) //negative difference means c_time > b_time { //do stuff } return 0; } 

Now, if you are a complete beginner, some of the things here are a little hard to understand. I can only recommend that you read a good C tutorial, everything will become clear. I hope you have enough time;)

+1
source

On Windows (as you seem to be using this OS) you can do something like this:

 #include <windows.h> #include <stdio.h> /* The date on which this program should continue running */ #define DAY 10 #define MONTH 12 /* 1 = Jan... 12 = Dec */ int main() { SYSTEMTIME t; GetLocalTime(&t); if (t.wDay != DAY || t.wMonth != MONTH) { printf("You can't open this program today!\n"); MessageBox(0, "You can't open this program today!", "Error", MB_ICONSTOP); return 1; } system("start https://www.youtube.com/watch?v=FchMuPQOBwA"); return 0; } 

The GetLocalTime () and SYSTEMTIME functions are located in windows.h, which you need to enable.

Or alternatively use the function time () from time.h, but in this case you need to convert the desired day to a UNIX timestamp (see http://en.wikipedia.org/wiki/Unix_time ) or convert the information returned by time () per day / month.

It was a simple program that would only run on that particular day, and exit with an error message if not. If you want to make a program that is installed on his computer when he launches it, and then waits for a certain time before opening a web page, this is much more complicated (you basically need to copy the EXE file somewhere on the system and add it to the registry, so that it starts automatically when you log in ... maybe no more than 30 lines of code, but not the easiest :-)).

To send it as an EXE so that it does not see the source code, you just need to compile it as if you were running it, and then send the EXE to it (make sure it does not require a runtime library from the compiler, check on another computer if it works correctly). Of course, if your dad looks into an EXE file with some kind of editor, he will see the address of the web page (but not easily, what is the condition that opens this page).

+1
source

Source: https://habr.com/ru/post/957415/


All Articles