Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created December 22, 2010 05:51
Show Gist options
  • Save johnhmj/751152 to your computer and use it in GitHub Desktop.
Save johnhmj/751152 to your computer and use it in GitHub Desktop.
// @eyny@Force[TW]@
#include "forcetw20101222.h"
//
CWChar::CWChar(void)
{
this->m_wcs = NULL;
this->m_size = 0;
}
CWChar::CWChar(const char* str)
{
int nSize = 0;
for (; str[nSize] != NULL; nSize ++);
this->m_size = nSize;
nSize ++;
this->m_wcs = new wchar_t[nSize];
//
// MultiByteToWideChar
// CP_ACP = 0
// MB_ERR_INVALID_CHARS = 0x00000008
MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str, nSize, this->m_wcs, nSize);
}
CWChar::~CWChar(void)
{
if ( this->m_wcs != NULL )
{
delete [] this->m_wcs;
}
}
void CWChar::setStr(const char* str)
{
int nSize = 0;
for (; str[nSize] != NULL; nSize ++);
this->m_size = nSize;
nSize ++;
this->m_wcs = new wchar_t[nSize];
//
// MultiByteToWideChar
// CP_ACP = 0
// MB_ERR_INVALID_CHARS = 0x00000008
MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, str, nSize, this->m_wcs, nSize);
}
CFindData::CFindData(void)
{
//
// INVALID_HANDLE_VALUE = ((HANDLE)(LONG_PTR)-1)
this->m_hFD = INVALID_HANDLE_VALUE;
memset((void*)&(this->m_FindData), 0, sizeof(WIN32_FIND_DATA));
}
CFindData::~CFindData(void)
{
//
// INVALID_HANDLE_VALUE = ((HANDLE)(LONG_PTR)-1)
this->m_hFD = INVALID_HANDLE_VALUE;
memset((void*)&(this->m_FindData), 0, sizeof(WIN32_FIND_DATA));
}
void CFindData::FindDir(void)
{
this->m_hFD = FindFirstFile(this->m_dir.m_wcs, &(this->m_FindData));
//
// INVALID_HANDLE_VALUE = ((HANDLE)(LONG_PTR)-1)
if ( this->m_hFD != INVALID_HANDLE_VALUE )
{
do
{
//
// FILE_ATTRIBUTE_DIRECTORY = 0x00000010
if ( this->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
//
// 不顯示根目錄
if ( ! ((wcscmp(this->m_FindData.cFileName, L".") == 0) | (wcscmp(this->m_FindData.cFileName, L"..") == 0)) )
{
wcout << "[" << this->m_FindData.cFileName << "]" << endl;
}
}
} while ( FindNextFile(this->m_hFD, &(this->m_FindData)) != 0 );
FindClose(this->m_hFD);
}
else
{
cout << "Error: find directory failed." << endl;
}
}
void CFindData::FindFile(void)
{
this->m_hFD = FindFirstFile(this->m_dir.m_wcs, &(this->m_FindData));
//
// INVALID_HANDLE_VALUE = ((HANDLE)(LONG_PTR)-1)
if ( this->m_hFD != INVALID_HANDLE_VALUE )
{
do
{
//
// FILE_ATTRIBUTE_DIRECTORY = 0x00000010
if ( this->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
//
// 僅顯示檔案
;
}
else
{
wcout << this->m_FindData.cFileName << endl;
}
} while ( FindNextFile(this->m_hFD, &(this->m_FindData)) != 0 );
FindClose(this->m_hFD);
}
else
{
cout << "Error: find file failed." << endl;
}
}
// @eyny@Force[TW]@
#ifndef _FORCETW20101222_H_
#define _FORCETW20101222_H_
#include <iostream>
#include <Windows.h>
#include <memory.h>
using namespace std;
//
class CWChar
{
public:
wchar_t* m_wcs;
int m_size;
CWChar(void);
CWChar(const char* str);
~CWChar(void);
void setStr(const char* str);
};
//
class CFindData
{
public:
CWChar m_dir;
HANDLE m_hFD;
WIN32_FIND_DATA m_FindData;
CFindData(void);
~CFindData(void);
void FindDir(void);
void FindFile(void);
};
//
#endif
// @eyny@Force[TW]@
// Integrated Development Environment
// Visual C++
#include <iostream>
#include <cstdlib>
#include <locale.h>
//#include <string>
#include "forcetw20101222.h"
using namespace std;
//
int main(int argc, char** argv)
{
setlocale(LC_ALL, "cht");
//
// 輸入目錄名稱
//string IptBfr;
//cout << "Input a directory: ", getline(cin, IptBfr);
//IptBfr += "\\*";
CFindData fd;
//fd.m_dir.setStr(IptBfr.c_str());
fd.m_dir.setStr("D:\\*");
fd.FindDir();
fd.FindFile();
//
//system("PAUSE");
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment