Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中暂停执行直到指定的时间_C_Time - Fatal编程技术网

在C语言中暂停执行直到指定的时间

在C语言中暂停执行直到指定的时间,c,time,C,Time,新来的程序员。喜欢超新的。我正在为我爸爸的生日写一个节目 #include <stdio.h> #include <time.h> int main() { int CurrentTime; system("start https://www.youtube.com/watch?v=FchMuPQOBwA"); return 0; } #包括 #包括 int main() { int当前时间; 系统(“启动https://www.youtube

新来的程序员。喜欢超新的。我正在为我爸爸的生日写一个节目

#include <stdio.h>
#include <time.h>

int main()
{
    int CurrentTime;
    system("start https://www.youtube.com/watch?v=FchMuPQOBwA");
    return 0;
}
#包括
#包括
int main()
{
int当前时间;
系统(“启动https://www.youtube.com/watch?v=FchMuPQOBwA");
返回0;
}
到目前为止我已经知道了。我该怎么做才能让他在生日或特定时间之前打开它?及时环顾四周。h和谷歌搜索了一下,但我似乎找不到解决办法。还有,我怎样才能把它发送给他,使它只是一个.exe,而他看不到代码


提前感谢

这里有一个例子,说明了在引用“time()”函数时需要使用的所有函数。

您需要执行的步骤:

  • 获取当前时间(c_时间)
  • 知道你爸爸生日的时间吗
  • (b_时间)检查c_时间是否大于b_时间
  • 以下是一个示例程序:

    #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's 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;
    }
    
    #包括/*printf*/
    #包括/*time\u t、struct tm、difftime、time、mktime*/
    int main()
    {
    时间t c_时间,b_时间;
    struct tm b_日期;
    双秒;
    y2k.tm_小时=0;y2k.tm_分钟=0;y2k.tm_秒=0;
    y2k.tm_year=100;y2k.tm_mon=0;y2k.tm_mday=1;//2000年1月1日。我会让你改变的,因为我不知道什么时候是重要的日子。
    时间(&c_time);/*获取当前时间;与:timer=time(NULL)相同*/
    b_时间=mktime(&b_日期);
    秒=离散时间(c_时间,b_时间);
    if(秒<0)//负差表示c_时间>b_时间
    {
    //做事
    }
    返回0;
    }
    
    现在,如果你是一个完全的初学者,这里的一些东西有点难理解。我只能建议你读一本好的C教程,一切都会变得清晰。我希望你有足够的时间;)

    在Windows上(您似乎在使用该操作系统),可以执行以下操作:

    #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;
    }
    
    #包括
    #包括
    /*此程序应继续运行的日期*/
    #定义第10天
    #定义第12个月/*1=1月。。。12=12月*/
    int main()
    {
    系统时间t;
    GetLocalTime&t;
    如果(t.wDay!=天| | t.wMonth!=月){
    printf(“您今天无法打开此程序!\n”);
    MessageBox(0,“您今天无法打开此程序!”,“错误”,MB_ICONSTOP);
    返回1;
    }
    系统(“启动https://www.youtube.com/watch?v=FchMuPQOBwA");
    返回0;
    }
    
    GetLocalTime()函数和SYSTEMTIME结构位于windows.h中,因此需要将其包括在内

    或者使用time.h中的time()函数,但在这种情况下,需要将所需的日期转换为UNIX时间戳(请参阅),或者将time()返回的信息转换为天/月

    这是一个简单的程序,只会运行在特定的一天,并退出与错误消息,如果没有。如果你想让一个程序在他运行时安装在他的计算机上,然后在打开网页之前等待特定的时间,那要复杂得多(基本上,您必须将EXE文件复制到系统中的某个位置,并将其添加到注册表中,以便在登录时自动运行…可能不超过30行代码,但不是最简单的;-))


    要将其作为EXE发送,以便他看不到源代码,那么,您只需将其编译为您想要运行它的样子,然后将EXE发送给他(确保它不需要来自编译器的运行库,请在另一台PC上检查它是否正确运行)。当然,如果你爸爸用编辑器查看EXE文件,他会看到网页的地址(但不容易看到打开此页面的条件)。

    +1个很酷的问题,但我认为对于一个新手程序员来说,这有点难。你必须处理一些复杂的事情。你说“在他生日或特定时间之前不能打开”是什么意思?你想让它在某个特定的时间打开浏览器作为一个惊喜吗?@cirosantilli我猜他的意思是检查日期,如果时间太早,那么说“今天还不是你的生日,等你生日到了再来吧”。您可以将其作为网站托管吗?这可能会更好。@cirosantilli我想他想暂停执行,直到某个时间我猜你在Windows上,所以你可以使用任务计划程序在某个时间打开浏览器。这很好。非常感谢你。如何找到运行它的.exe。我用的是代码块。我知道如何编译它。但是.exe在哪里?当使用代码块时,请确保在发布模式下编译,以便获得更小的exe。您应该在项目所在的名为Release的目录中找到它。如果在调试模式下编译,该目录将命名为debug。注意,因为代码块使用MinGW的gcc作为编译器,而最新版本需要一个DLL来运行编译后的程序(通常是libgcc_s_dw2-1.DLL)。为了避免这种情况,您应该在静态模式下编译(选中选项),或者使用仍然使用gcc版本3的旧版本的MinGW。
    MB_ICONSTOP
    :当再次打开此应用程序时,他的父亲会感到害怕;-)(开玩笑)