C++总结(零)

1.判断数据类型

头文件:#include <typeinfo>

cout << typeid(value).name() << endl;

2.C++解决排列问题

思路其实很简单,写几个swap函数,将数组传递过去即可更新下一个排列的数组。

3.读取目录下所有文件(C++实现)

#undef UNICODE // 如果你不知道什么意思,请不要修改
#define MAX_RESULT 256

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>


char** EnumFiles(const char *directory, int *count)
{
	//调用win api;
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;//句柄
	char result[MAX_RESULT][MAX_PATH];//定义二维数组
	char **returnresult;
	char pattern[MAX_PATH];
	int i = 0, j;

	// 开始查找
	strcpy(pattern, directory);
	strcat(pattern, "\\*.*");
	hFind = FindFirstFile(pattern, &FindFileData);

	if (hFind == INVALID_HANDLE_VALUE)
	{
		*count = 0;
		return NULL;
	}
	else
	{
		do
		{
			strcpy(result[i++], FindFileData.cFileName);
		} while (FindNextFile(hFind, &FindFileData) != 0);
	}

	// 查找结束
	FindClose(hFind);

	// 复制到结果中
	returnresult = (char **)calloc(i, sizeof(char *));

	for (j = 0; j < i; j++)
	{
		returnresult[j] = (char *)calloc(MAX_PATH, sizeof(char));
		strcpy(returnresult[j], result[j]);
	}

	*count = i;
	return returnresult;
}

void main()
{
	int i, count;
	char ** result;
	char directory[MAX_PATH];

	printf("请输入要查询的文件夹:");
	scanf("%s", directory);

	result = EnumFiles(directory, &count);

	for (i = 0; i < count; i++)
		printf("%s\n", result[i]);
	system("pause");

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值