牛骨文教育服务平台(让学习变的简单)
博文笔记

巩固C语言(五)----获取CMD输出的字符串 & 字符串的拷贝、查找、长度 & 实例之调戏QQ

创建时间:2016-04-15 投稿人: 浏览次数:842

#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("
strlen(CMDout) = %d
", strlen(CMDout));

	fprintf(my_out, "%s", CMDout);
	
	fclose(my_out);
	system("pause");
}

在out.txt中将会出现和CMD中一样的输出字符串。



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, 控制字符串,参数表)

#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 = "";
}

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
", strLength(str));
	strCat(str, sour);
	printf("*str = %s
", str);
	printf("length = %d
", strLength(str));
	char find[10] = "Lin";
	char *p = StrStr(str, find);
	if (p == NULL)
		printf("Not find!
");
	else
		printf("Find!
");

	system("pause");
}

运行结果:

length = 9
*str = Apple Lin Qian Qin
length = 18
Find!
请按任意键继续. . .



#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 = "";
}

//需要使用下标
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 != ""; tem_dest++)
	{
		int flag = 1;
		for (char *tem_str = str; *tem_str != ""; tem_str++)
		{
			if (*(tem_dest + (tem_str - str)) == "")
			{
				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");
}


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。