获取文件夹下所有文件(在不定义Unicode宏情况下)

// FindFile.cpp : 定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include "windows.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define MAC_FILENAMELENOPATH 100
void FindFile(TCHAR * Path);
void AnToUn(wchar_t * wszString);
FILE *fp;
int main(int argc, char *argv[])
{
	//setlocal(
	 fp = fopen("E:\\SogouC_save.txt","w+");
	 FindFile(L"E:\\SogouC");
 	 system("pause");
	 return (0);
	 fclose(fp);

}
void FindFile(TCHAR * Path)
{	

	TCHAR Suffix[5]=L"\\*.*";
	
	TCHAR PathOut[256];
	wcscpy(PathOut,Path);
	wcscat(PathOut,L"\\");

	TCHAR * cp = Path;
	TCHAR p[256];
	wcscpy(p,Path);
	wcscat(p,L"\\*.*");  
	 	
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;
	TCHAR *FilePathBuff = p;
	hFind = FindFirstFile(FilePathBuff, &FindFileData);	
	
	if((hFind = FindFirstFile(p, &FindFileData)) != INVALID_HANDLE_VALUE){
		do{
  			if(wcscmp(FindFileData.cFileName, L".") == 0 || wcscmp(FindFileData.cFileName, L"..") == 0){
				continue;
			}
				if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
				TCHAR * cp = FindFileData.cFileName;
				wchar_t p[256];
				wcscpy(p,Path);
				wcscat(p,L"\\");
				wcscat(p,FindFileData.cFileName);
				FindFile(p);
			}
			else{
				 setlocale(LC_ALL, "");//这是stdio的
				_tprintf(TEXT("%s%s\n"),PathOut,FindFileData.cFileName);
				wchar_t Pathtemp[256];
				wcscpy(Pathtemp,PathOut);
				wcscat(Pathtemp,FindFileData.cFileName);
				AnToUn(Pathtemp);
			}
		}while(FindNextFile(hFind, &FindFileData) != 0);
		FindClose(hFind);
	}
	
//fclose(fp);
}
void AnToUn(wchar_t * wszString)
  {
      // unicode to ansi
    //wchar_t* wszString = L"abcd1234你我他";
   //预转换,得到所需空间的大小,这次用的函数和上面名字相反
    int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
    //同上,分配空间要给'\0'留个空间
    char* szAnsi = new char[ansiLen + 1];
    //转换
   //unicode版对应的strlen是wcslen
    ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
    //最后加上'\0'
    szAnsi[ansiLen] = '\0';
	fwrite(szAnsi,ansiLen*sizeof(char),1,fp);
	fwrite("\n",1,1,fp);
 delete[] szAnsi;   
 szAnsi =NULL;
}

你可能感兴趣的:(File,null,delete,Access,Path,FP)