在特定时间使用C+打开链接+; 我试图写一个C++控制台程序,它在特定的时间打开与Web浏览器的链接。时钟更新良好,外壳线打开了链接,但我就是不能让这两个一起工作。这是我的密码: #include <Windows.h> #include <shellapi.h> #include <ctime> #include <chrono> #include <thread> using namespace std; int main(int argc, char* argv[]) { string url = "https://www.google.com"; while (true) { system("cls"); time_t now = time(0); char* dt = ctime(&now); cout << dt; while (dt == "Mon May 18 09:48:00 2020") { ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL); } std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } return 0; } #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { 字符串url=”https://www.google.com"; while(true){ 系统(“cls”); 现在时间=时间(0); char*dt=ctime(&now); 至少对我来说,你的代码似乎有点混乱。不清楚为什么我们会一次睡一秒钟,直到达到目标。更糟糕的是,如果其中一个睡眠调用时间过长,我们可能会打过头,永远不会执行所需的操作。更糟糕的是,如果我们确实达到了所需的时间,内部循环看起来会出错这是一个无限循环,因为只要字符串有所需的时间,它就会继续重新执行——但我们不会将字符串更新为循环中的当前时间,因此如果我们确实命中了该时间,字符串将永远保持不变
至少根据您的描述,我们只想选择一个时间,在此之前睡眠,然后执行该命令。为此,按照此一般顺序编写的代码似乎就足够了:在特定时间使用C+打开链接+; 我试图写一个C++控制台程序,它在特定的时间打开与Web浏览器的链接。时钟更新良好,外壳线打开了链接,但我就是不能让这两个一起工作。这是我的密码: #include <Windows.h> #include <shellapi.h> #include <ctime> #include <chrono> #include <thread> using namespace std; int main(int argc, char* argv[]) { string url = "https://www.google.com"; while (true) { system("cls"); time_t now = time(0); char* dt = ctime(&now); cout << dt; while (dt == "Mon May 18 09:48:00 2020") { ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL); } std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } return 0; } #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { 字符串url=”https://www.google.com"; while(true){ 系统(“cls”); 现在时间=时间(0); char*dt=ctime(&now); 至少对我来说,你的代码似乎有点混乱。不清楚为什么我们会一次睡一秒钟,直到达到目标。更糟糕的是,如果其中一个睡眠调用时间过长,我们可能会打过头,永远不会执行所需的操作。更糟糕的是,如果我们确实达到了所需的时间,内部循环看起来会出错这是一个无限循环,因为只要字符串有所需的时间,它就会继续重新执行——但我们不会将字符串更新为循环中的当前时间,因此如果我们确实命中了该时间,字符串将永远保持不变,c++,url,time,hyperlink,console,C++,Url,Time,Hyperlink,Console,至少根据您的描述,我们只想选择一个时间,在此之前睡眠,然后执行该命令。为此,按照此一般顺序编写的代码似乎就足够了: #include <windows.h> #include <chrono> #include <thread> #include <iostream> int main() { // For the moment, I'm just going to pick a time 10 seconds into the futu
#include <windows.h>
#include <chrono>
#include <thread>
#include <iostream>
int main() {
// For the moment, I'm just going to pick a time 10 seconds into the future:
time_t now = time(nullptr);
now += 10;
// compute that as a time_point, then sleep until then:
auto then = std::chrono::system_clock::from_time_t(now);
std::this_thread::sleep_until(then);
// and carry out the command:
ShellExecute(NULL, "open", "https://www.google.com",
NULL, NULL, SW_SHOWNORMAL);
}
另外:如果我希望程序在此之后继续运行并存储一个新的时间,它将打开另一个链接?可能吗?@VVahla:当然。一旦
ShellExecute
启动目标程序,您的父程序将继续正常运行,因此您可以做任何您想做的事情。
#include <windows.h>
#include <chrono>
#include <thread>
#include <iostream>
#include <sstream>
#include <iomanip>
auto convert(std::string const &s) {
std::istringstream buffer(s);
struct tm target_tm;
buffer >> std::get_time(&target_tm, "%a %b %d %H:%M:%S %Y");
time_t target = mktime(&target_tm);
return std::chrono::system_clock::from_time_t(target);
}
int main() {
auto target = convert("Mon May 18 09:48:00 2020");
std::this_thread::sleep_until(target);
ShellExecute(NULL, "open", "https://www.google.com",
NULL, NULL, SW_SHOWNORMAL);
}