路径与文件、文件遍历

//下面的一段代码主要是获得当前程序的运行目录(.exe)所在的目录
{
    CString path; 
    GetModuleFileName(NULL,path.GetBufferSetLength(MAX_PATH+1),MAX_PATH); 
    path.ReleaseBuffer(); 
    int pos = path.ReverseFind('\\'); 
    path = path.Left(pos); 

 

}

 

 

//判断文件夹是否存在,不存在则创建

CString csPath(TEXT(“E:\\Path”));
if (!PathIsDirectory(路径))
{
         CreateDirectory(csPath, 0);//不存在则创建

}

 

删除文件或目录

下面的程序演示了如何使用remove()函数删除文件。

#include<stdio.h>
int main(){
    char filename[80];
    printf("The file to delete:");
    gets(filename);
    if( remove(filename) == 0 )
        printf("Removed %s.", filename);
    else
        perror("remove");
}

 

宽字节的cstring转char*

 

CString st=_T("123");

 int nLength = st.GetLength();
 int nBytes = WideCharToMultiByte(CP_ACP,0,st,nLength,NULL,0,NULL,NULL);
 char* path1 = new char[ nBytes + 1];
 memset(path1,0,nLength + 1);
 WideCharToMultiByte(CP_OEMCP, 0, st, nLength, path1, nBytes, NULL, NULL); 
 path1[nBytes] = 0; 
 int nBytes = WideCharToMultiByte(CP_ACP,0,st,nLength,NULL,0,NULL,NULL);
 char* path1 = new char[ nBytes + 1];
 memset(path1,0,nLength + 1);
 WideCharToMultiByte(CP_OEMCP, 0, st, nLength, path1, nBytes, NULL, NULL); 
 path1[nBytes] = 0; 

 

//遍历文件夹下的文件方法1

#include <afx.h>

int main()
{
	CString pathStr;
	GetModuleFileName(NULL, pathStr.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
	pathStr.ReleaseBuffer();
	int pos = pathStr.ReverseFind('\\');
	pathStr = pathStr.Left(pos);
	CStringArray arrStrFile;
	int num = getFilepath(pathStr, arrStrFile);

return 0;
}

int getFilepath(char *pathStr, char arrStrFile[][512])
{

	CString myDataPath, fdPath;   //设置路径变量
	myDataPath = pathStr + "\\*.*"; //文件夹路径
	CString strTmp;      //后缀名变量

	CFileFind find;		//例化CFileFind
	BOOL bf = find.FindFile(myDataPath);	//
	while (bf)
	{
		bf = find.FindNextFile();
		if (!find.IsDots())
		{
			fdPath = find.GetFilePath();
			if (find.IsDirectory())
			{
				//如果是文件夹,递归,继续往下找                        
				getFilepath(fdPath, arrStrFile);
			}
			else
			{
				//如果是文件,判断是否是*.txt
				strTmp = fdPath.Right(4);  //取后缀名
				strTmp.MakeLower();		//字符串小写化
				if (strTmp == ".bmp")
					arrStrFile.Add(fdPath);
			}
		}
	}
	find.Close();

	fileNum = arrStrFile.GetSize();

	return fileNum;
}

//遍历文件夹下的文件方法2

#include <stdio.h>
#include <direct.h> 
#include <io.h>
int getFilepath(char *pathStr, char arrStrFile[][512]);

char arrStrFile[8000][512];
int main()
{
	char* pathStr = _getcwd(NULL, 0);/*获取当前运行目录*/
	char imgpath[1024];
	char path1[1024];
	char path2[1024];
	strcpy(imgpath, pathStr);
	strcat(pathStr,"\\IMG\\*.*"); /*连接字符串*/
	strcat(imgpath, "\\IMG\\");
	int num = getFilepath(pathStr, arrStrFile);
	

    return 0;
}


int getFilepath(char *pathStr, char arrStrFile[][512])
{
	int fileNum = 0;
	_finddata_t fileDir;
	long lfDir;  //x64环境下改为 intptr_t lfDir  否则报错

	if ((lfDir = _findfirst(pathStr, &fileDir)) == -1l)
		printf("No file is found\n");
	else 
	{
		printf("file list:\n");
		do
		{
			if (strcmp(fileDir.name, ".") == 0 || strcmp(fileDir.name, "..") == 0) continue;
			printf("%s\n", fileDir.name);
			strcpy(arrStrFile[fileNum], fileDir.name);
			fileNum++;
		} while (_findnext(lfDir, &fileDir) == 0);
	}
	_findclose(lfDir);

	return fileNum;
}

 

//遍历文件夹下的文件方法3

 

#include <stdio.h>
#include <direct.h> 
#include <io.h>
#include<Windows.h>
#include<atlbase.h>

int getFilepath(char *pathStr, char arrStrFile[][512]);

char arrStrFile[8000][512];
int main()
{
	char* pathStr = _getcwd(NULL, 0);/*获取当前运行目录*/
	char imgpath[1024];
	char path1[1024];
	char path2[1024];
	strcpy(imgpath, pathStr);
	strcat(pathStr,"\\IMG\\*.*"); /*连接字符串*/
	strcat(imgpath, "\\IMG\\");
	int num = getFilepath(pathStr, arrStrFile);
	

    return 0;
}


int getFilepath(char *pathStr, char arrStrFile[][512])
{
int fileNum = 0;

	HANDLE hFind;
	WIN32_FIND_DATA findData;
	LARGE_INTEGER size;
	USES_CONVERSION;
	WCHAR* dire = A2W(pathStr);

	hFind = FindFirstFile(dire,&findData);

	if (hFind == INVALID_HANDLE_VALUE) 
		return 0;

	do
	{
		if (strcmp(W2A(findData.cFileName), ".") == 0 || strcmp(W2A(findData.cFileName), "..") == 0)
			continue;
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//是否是目录
		{
			printf("<dir> %s", W2A(findData.cFileName));
		}
		else
		{
			size.LowPart = findData.nFileSizeLow;
			size.HighPart = findData.nFileSizeHigh;

			strcpy(arrStrFile[fileNum], W2A(findData.cFileName));
			fileNum++;
		}
	} while (FindNextFile(hFind, &findData));

	return fileNum;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值