巩固C语言(五)----获取CMD输出的字符串 & 字符串的拷贝、查找、长度 & 实例之调戏QQ
来源:互联网 发布:360.cn 域名价格 编辑:程序博客网 时间:2024/05/01 07:30
1 获取CMD中字符串的输出:
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>void getCMD(char *in, char *out){char buffer[128] = { 0 };FILE *pipe = _popen(in, "r");//获取命令行的输入,并执行,pipe相当于一个管道if (!pipe)exit(-1);while (!feof(pipe))//判断文件是否结束{if (fgets(buffer, 128, pipe))//从管道中读取数据{strcat(out, buffer);}}_pclose(pipe);//关闭该管道}void main(){FILE *my_out = fopen("out.txt", "w");if (!my_out)exit(-1);char CMDin[50] = { 0 };char CMDout[4096] = { 0 };scanf("%s", CMDin);getCMD(CMDin, CMDout);printf("打印输出:%s", CMDout);printf("\nstrlen(CMDout) = %d\n", strlen(CMDout));fprintf(my_out, "%s", CMDout);fclose(my_out);system("pause");}
在out.txt中将会出现和CMD中一样的输出字符串。
2 与文件相关的指令
C语言程序在进行文件操作时遵循如下操作步骤:打开读写操作关闭,通俗地说,打开是获取文件结构、系统为文件分配缓冲区的过程,不打开文件就不能对其进行读写,关闭是释放缓冲区和其他资源的过程,不关闭文件就会慢慢耗光系统资源,。在进行文件操作时,系统自动与3个标准设备文件联系,这3个文件无需打开和关闭,它们的文件指针分别是:stdin:标准输入文件指针,系统分配为键盘。stdout:标准输出文件指针,系统分配为显示器。stderr:标准错误输出文件指针,系统分配为显示器。举例来说,从文件输入和向文件输出有两个对应函数fscanf和fprintf,两个函数的原型分别为:int fprintf(FILE* ofp,控制字符串,参数表);int fscanf(FILE* ifp ,控制字符串,参数表);参数表中参数的个数同样是任意的,fprintf函数用于将转换后的控制字符串写出到ofp指向的文件中,fscanf用于从ifp指向的文件中读取字节信息为参数表中的参数赋值。前面章节中用到的标准输入输出函数printf和scanf实际上等价于:fprintf(stdout, 控制字符串,参数表)fscanf(stdin, 控制字符串,参数表)
3 字符串的拷贝、查找、长度
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>int strLength(const char *str){int num = 0;while (*str){num++;str++;}return num;}void strCat(char *dest, const char *sour){//注意当 char *dest = "Apple Lin"时会发生错误,因为不知道dest最多能承载多少字符//当然遇到这种情况时可以用realloc函数实现int destLen = strLength(dest);int sourLen = strLength(sour);char *temp = dest + destLen;while (*sour){*temp = *sour;temp++;sour++;}*temp = '\0';}char * StrStr(char *sour, char *find){char *p = NULL;//要返回的地址指针int sourLen = strLength(sour);int findLen = strLength(find);for (int i = 0; i < sourLen - findLen; i++){int flag = 1;//标识符for (int j = 0; j < findLen; j++){if (sour[i + j] != find[j]){flag = 0;break;}}if (flag == 1)//找到了{p = &sour[i];break;}}return p;}void main(){char str[40] = "Apple Lin";char sour[20] = " Qian Qin";printf("length = %d\n", strLength(str));strCat(str, sour);printf("*str = %s\n", str);printf("length = %d\n", strLength(str));char find[10] = "Lin";char *p = StrStr(str, find);if (p == NULL)printf("Not find!\n");elseprintf("Find!\n");system("pause");}
运行结果:
length = 9*str = Apple Lin Qian Qinlength = 18Find!请按任意键继续. . .
4 调戏QQ----找到QQ并将之关闭
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>int strLength(const char *ch){int num = 0;while (*ch){num += 1;ch++;}return num;}void strCat(char *dest, const char *sour){int destLen = strLength(dest);int sourLen = strLength(sour);char *temp = dest + destLen;while (*sour){*temp = *sour;temp++;sour++;}*temp = '\0';}//需要使用下标char * strFind(char *dest, char *str){char *p = NULL;int destLen = strLength(dest);int strLen = strLength(str);for (int i = 0; i < destLen - strLen; i++){int flag = 1;for (int j = 0; j < strLen; j++){if (dest[i + j] != str[i]){flag = 0;break;}}if (flag == 1){p = dest + i;break;}}return p;}//不需要使用下标char * strFind2(char *dest, char *str){char *p = NULL;for (char *tem_dest = dest; *tem_dest != '\0'; tem_dest++){int flag = 1;for (char *tem_str = str; *tem_str != '\0'; tem_str++){if (*(tem_dest + (tem_str - str)) == '\0'){flag = 0;break;}if (*(tem_dest + (tem_str - str)) != *tem_str){flag = 0;break;}}if (flag == 1){p = tem_dest;break;}}return p;}void receCMD(char *cmd, char *out){FILE *pipe = _popen(cmd, "r");char buffer[128] = { 0 };if (!pipe)exit(-1);while (!feof(pipe)){if(fgets(buffer, 128, pipe))strCat(out, buffer);}_pclose(pipe);}void main(){char in[40] = "tasklist";char out[9192] = { 0 };/*printf("请您输入指令:");scanf("%s", in);*/receCMD(in, out);char *p = strFind2(out, "QQ.exe");if (p){printf("QQ存在,关闭之...!");system("taskkill /f /im QQ.exe");}elseprintf("QQ不存在!");system("pause");}
0 0
- 巩固C语言(五)----获取CMD输出的字符串 & 字符串的拷贝、查找、长度 & 实例之调戏QQ
- C语言---递归反向输出任意长度的字符串
- C语言 字符串的输出
- 获取字符串的长度!
- 获取字符串的长度
- 获取字符串的长度
- 字符串长度的获取
- C语言-字符串的拷贝指针
- C语言--字符串长度的计算方法
- C语言计算字符串的长度strlen
- C语言字符串的长度与大小
- C语言 - 得到字符串的长度
- C语言中字符串的长度
- 【c语言】求一个字符串的长度
- C语言基础 计算字符串的长度
- C语言:返回传入字符串的长度
- c语言:字符串的连接,复制,长度
- 简单c语言调戏qq
- MyBatis学习记录(4):MyBatis配置文件的优化
- 杭电2132
- 数据结构
- ActivePerl从源码安装模块
- HDU 1019 Least Common Multiple(求最小公倍数)
- 巩固C语言(五)----获取CMD输出的字符串 & 字符串的拷贝、查找、长度 & 实例之调戏QQ
- 杭电5660
- codeforces
- Apache 的动态静态编译 &工作模式MPM
- Git命令总结
- 生成器模式
- Spring笔记整理
- 【Leetcode】2.Add Two Numbers 解题
- ZOJ-3752-The Three Guys【暴力】