当前位置: 首页 > news >正文

如何用代码将网页打开

好久没更新了,因为快中考了,所以这时间只能月更甚至更久

先说说要使用的头文件:

#include<windows.h>

函数是ShellExecuteA,比如 

ShellExecuteA(NULL, "open", "https://www.baidu.com", NULL, NULL, SW_SHOWNORMAL);

 显而易见,格式是

ShellExecuteA(NULL, "open", "网址", NULL, NULL, SW_SHOWNORMAL);

其他参数我们都不用管 

 比如看下面例子

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>int main() {char searchQuery[100];printf("请输入要搜索的文案:");fgets(searchQuery, sizeof(searchQuery), stdin);searchQuery[strcspn(searchQuery, "\n")] = '\0';char url[200];sprintf_s(url, sizeof(url), "https://www.baidu.com/s?wd=%s", searchQuery);ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);return 0;
}

为了防止有些人看不懂,我们先讲讲其中的函数

1.fgets函数

要说的话,跟getchar差不多,函数原型是

 fgets(char *_Buffer, int _MaxCount, FILE *_Stream)

这个函数会输入字母,输入的大小会根据参数(不包括空格)来输入,最后一个参数基本都填stdin

比如这个例子

#include <stdio.h>int main() {char str[10];printf("输入一个字符串:");fgets(str, sizeof(str), stdin);printf("你输入的字符串是:%s", str);return 0;
}

显而易见,就是先创建一个字符串str,然后使用fgets函数输入,最后输出,比如我们输入"apple",程序就会也输出"apple",但输入"an apple的话,只会输出"an appl",这是因为这个函数只会识别到字符(不包括空格)的数量一共是7个,所以只会输出字符串的前7个字符

2.strcspn函数

先说说函数原型

strcspn(const char *_Str, const char *_Control)

这个函数用于输出第二个参数的第一个参数的位置,比如

#include <string.h>
#include <iostream>int main() {char mystr[] = "hello c++ and world";char mychar = 'w';std::cout << mychar << "in the str index is " << strcspn(mystr, "w");
}

输出

w the str index is 14

3.sprintf_s

函数原型

sprintf_s(char *_Buffer, size_t _BufferCount, const char *_Format, ...)

这个函数顾名思义,跟printf差不多,比如

#include <cstdio>
#include <iostream>int main(){char name[] = "John";int age = 30;char buffer[100];sprintf_s(buffer, sizeof(buffer), "Hello, %s! You are %d years old.", name, age);for (char mychar : buffer)std::cout << mychar;
}

输出

Hello, John! You are 30 years old.

这个函数的前两个参数是获取字符串字符串和的它的区域,后三个参数跟printf的用法一样,有占位符和参数

4.回归程序

回到

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>int main() {char searchQuery[100];printf("请输入要搜索的文案:");fgets(searchQuery, sizeof(searchQuery), stdin);searchQuery[strcspn(searchQuery, "\n")] = '\0';char url[200];sprintf_s(url, sizeof(url), "https://www.baidu.com/s?wd=%s", searchQuery);ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);return 0;
}

这个程序中,我们先创造了字符串searchQuery,后面使用fgets函数输入搜索内容,因为这个函数中会读取到"\n"(换行符),所以我们要用strcspn函数找到"\n"(换行符)来把换行符换成"\0'(终止符),然后创建字符串url,然后使用sprintf_s函数取使url赋值为搜索网址,最后打开网页

5.结尾

当然,你可以在此基础上把搜索网址改变,比如b站,yahoo,bing都行。


http://www.mrgr.cn/news/54172.html

相关文章:

  • AI 自学 Lesson2 - 回归(Regression)
  • 监控易监测对象及指标之:Kafka中间件JMX监控指标解读
  • 利用 PyTorch 进行深度学习训练过程中模型的 .eval() 和 .train() 属性介绍
  • SpringBoot+MyBatis+MySQL项目基础搭建
  • vue前端开发框架的常见知识点和应用
  • es 全文文本分词查询
  • 2024年第四届“网鼎杯”网络安全大赛-赛前模拟训练
  • 空开老化损坏更换
  • MatLab Desired port was :31515解决方案
  • 关于串口中断时的一些问题
  • C++ Socket通讯连接Server,并可设置连接超时时间
  • 2060:【例1.1】计算机输出
  • 【Java】—一篇搞懂POJO和VO、BO、DTO、PO模型
  • 程序员是否要考研?结合当前环境和形势的分析
  • linux线程 | 全面理解同步与互斥 | 同步
  • SpringBoot配置文件敏感信息如何加密?及踩坑总结(全面讲解)
  • 【Python爬虫实战】深入解析BeautifulSoup4的强大功能与用法
  • JavaScript 字符串常用方法
  • USART串口(发送和接收)
  • 【MySQL】多表查询——内连接,左/右连接
  • MyBatisPlus
  • 【算法】将单向链表按某值分成左边小、中间相等、右边大的形式
  • 在各大媒体报纸上刊登自己的文章用什么投稿方法发表快?
  • RDK X5 目标跟踪核心代码Deepsort
  • Flux.concat 使用说明书
  • 注塑机机械手升降传送机程序