TCHAR CHAR WCHAR转换 和 获取一个目录下所有文件名及修改特定文件

 
#ifndef  _GETFILE_H
#define  _GETFILE_H

#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <TCHAR.h>

using namespace std;

/**
//TCHAR是一个宏,可能是char也可能是wchar_t,看编译参数是否定义了UNICODE
#ifdef   UNICODE     
typedef   wchar_t   TCHAR;    
#else     
typedef   unsigned   char   TCHAR;    
#endif   

typedef   unsigned   char   CHAR;     
typedef   unsigned   wchar_t   WCHAR;   
*/

//把TCHAR数组转换为char数组
char* Wide2Bytes(TCHAR *pWide)
{
#ifdef _UNICODE
	int iLen = 2*wcslen((const wchar_t*)pWide);
	char *chRtn = new char[iLen+1];
	//Converts a sequence of wide characters to a corresponding sequence of multibyte characters
	wcstombs(chRtn,(const wchar_t*)pWide,iLen+1);
	return chRtn;
#else
	return (char*)pWide;
#endif
}

//将char数组转换为TCHAR数组
wchar_t* CharToWChar(char* szInput);
TCHAR* CharToTChar(char *szInput)
{
	_ASSERT(szInput);
#ifdef _UNICODE
	return CharToWChar(szInput);
#else
	char *szTmp = new char[strlen(szInput)+1];
	if(!szTmp)
		return 0;
	::strcpy(szTmp,szInput);
	return szTmp;
#endif
}

wchar_t* CharToWChar(char* szInput)
{
	_ASSERT(szInput);
	int iLen = strlen(szInput)+1;
	wchar_t *Tmp = new wchar_t[iLen];
	mbstowcs(Tmp,szInput,iLen);
	return Tmp;
}


int main()
{
	cout << sizeof(CHAR) << endl;
	cout << sizeof(WCHAR) << endl;
	//TCHAR的大小会随着是否定义了UNICODE宏而变化,可以通过工程属性设置
	//编译时候使用的字符集
	cout << sizeof(TCHAR) << endl;

	char *cstr = "hello";
	wprintf(CharToTChar(cstr));//注意必须使用wprintf
	wprintf(L"\n");

	//这样必须是使用UNICODE字符集进行编译,在头文件TCHAR.h中
	//TCHAR testT[] = _T("hello");
	TCHAR testT[] = L"world";
	printf(Wide2Bytes(testT));
	printf("\n");
	

	//修改文件名,当前目录
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;
	/*
	char oldname[] = "read.txt";
	char newname[] = "new.txt";
	int result;
	result = rename(oldname,newname);
	if (0 == result)
		printf("Successfully!\n");
	else
		printf("Error Renaming File\n");
	*/

	//获取一个文件夹下的所有目录的名称,
	/*
	int find = 1;
	//hFind = FindFirstFile("c:\\*.*",&FindFileData);
	hFind = FindFirstFile("*.txt",&FindFileData);
	if (hFind == INVALID_HANDLE_VALUE)
	{
		printf("Invalid File Handle. GetLastError reports %d\n",GetLastError());
		return 0;
	}
	else
	{
		while (find)
		{
			printf("The First file found is %s\n",FindFileData.cFileName);
			find = FindNextFile(hFind,&FindFileData);	
		}
		FindClose(hFind);
	}
	*/

	//获取某路径下的特定文件并修改文件名
	/*
	char *oldfile;
	char *newfile = "replace.txt";
	int result = 1;
	hFind = FindFirstFile("aaaa.txt",&FindFileData);//第一个参数可以是一个具体路径
	if (hFind == INVALID_HANDLE_VALUE)
	{
		printf("Invalid File Handle. GetLastError reports %d\n",GetLastError());
		return 0;
	}
	oldfile = FindFileData.cFileName;
	result = rename(oldfile,newfile);
	if (0 == result)
		printf("Successfully!\n");
	else
		printf("Error Renaming File\n");
	FindClose(hFind);
	*/

	return 1;
}
/************************************************************************/
/* FindFirstFile(LPCSTR lpFileName,LPWIN32_FIND_DATA lpFindFileData):
lpFileName 
[in] Pointer to a null-terminated string that specifies a valid directory or path and file name, which can contain wildcard characters, such as * and ?. 
There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the FindFirstFile function parses paths.
lpFindFileData 
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory
Return Values
A search handle, used in a subsequent call to FindNextFile or FindClose, indicates success. INVALID_HANDLE_VALUE indicates failure. To get extended error information, call GetLastError. 


FindNextFile(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindData)
hFindFile 
[in] Search handle returned by a previous call to the FindFirstFile or the FindFirstFileEx function. 
lpFindFileData 
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory. The structure can be used in subsequent calls to FindNextFile to refer to the found file or directory. 
Return Values
Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES. 

WIN32_FIND_DATA:
typedef struct _WIN32_FIND_DATA {  
		DWORD dwFileAttributes; 
		FILETIME ftCreationTime;   
		FILETIME ftLastAccessTime; 
		FILETIME ftLastWriteTime;   
		DWORD nFileSizeHigh;   
		DWORD nFileSizeLow; 
		DWORD dwOID;   
		TCHAR cFileName[MAX_PATH]; 
} WIN32_FIND_DATA; 
*/
/************************************************************************/



#endif //_GETFILE_H

你可能感兴趣的:(function,File,search,Path,wildcard,structure)