巩固C语言(五)----获取CMD输出的字符串 & 字符串的拷贝、查找、长度 & 实例之调戏QQ
2016-04-15 23:58
375 查看
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"); else printf("Find!\n"); system("pause"); }
运行结果:
length = 9 *str = Apple Lin Qian Qin length = 18 Find! 请按任意键继续. . .
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"); } else printf("QQ不存在!"); system("pause"); }
相关文章推荐
- 【Leetcode】2.Add Two Numbers 解题
- 【Leetcode】1.Two Sum 解题
- 探究printf
- C# 与 C++ 交互
- (LeetCode 326)Power of Three
- (C语言基础版)贪吃蛇-支持滚键盘
- C++给函数传数组参数
- C++ <string><string.h>函数区别
- C++第二次作业补充
- C/C++语言中NULL、'\0’和0的区别
- C++/C代码规范(个人)(不断更新)
- C++第二次实验补充
- C++ 命名空间 namespace 知多少
- C++ 中关于默认构造函数的一点困惑
- VS 创建 使用C++ 静态类库(Dll)
- effective c++读书笔记(一)
- C++实践参考:数组作数据成员
- c++ cin和cout对象
- C++中的小错误
- 2015级C++第8周项目 数组和指针