如何遍历指定目录下的所有AVI文件, 取得它们的文件名并放入一文件名数组中去? 遍历目录,_findfirst,_findnext,复制文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #ifndef FILE_SEARCH#define FILE_SEARCH#include <stdio.h>#include <stdlib.h>#include <string.h>#include <direct.h>#include <io.h>#include "StackPP.h"typedef _finddata_t FINDDATA;typedef _finddata_t* LPFINDDATA;typedef StackTempl < _finddata_t* > FindDataStack;class FileSearch{protected: char* m_cPath;protected: ~FileSearch() { delete []m_cPath; m_cPath = NULL; } virtual void ProcessFile( LPFINDDATA p, char* workpath ) = 0;public: FileSearch( char* cPath ) { m_cPath = NULL; if( cPath != NULL ) { long lLength = strlen( cPath ); m_cPath = new char[lLength+1]; strcpy( m_cPath, cPath ); } } void Run() { char oldworkpath[_MAX_PATH]; _getcwd( oldworkpath, _MAX_PATH ); // ± #ifndef FILE_SEARCH#define FILE_SEARCH#include <stdio.h>#include <stdlib.h>#include <string.h>#include <direct.h>#include <io.h>#include "StackPP.h"typedef _finddata_t FINDDATA;typedef _finddata_t* LPFINDDATA;typedef StackTempl < _finddata_t* > FindDataStack;class FileSearch{protected: char* m_cPath;protected: ~FileSearch() { delete []m_cPath; m_cPath = NULL; } virtual void ProcessFile( LPFINDDATA p, char* workpath ) = 0;public: FileSearch( char* cPath ) { m_cPath = NULL; if( cPath != NULL ) { long lLength = strlen( cPath ); m_cPath = new char[lLength+1]; strcpy( m_cPath, cPath ); } } void Run() { char oldworkpath[_MAX_PATH]; _getcwd( oldworkpath, _MAX_PATH ); // 保存当前工作路径 FindDataStack stack; FINDDATA c_file; strcpy( c_file.name, m_cPath ); LPFINDDATA p = new FINDDATA; memcpy( p, &c_file, sizeof(FINDDATA) ); stack.push (p); char workpath[_MAX_PATH]; while( (p = stack.pop()) != NULL ) { _chdir( p->name ); _getcwd( workpath, _MAX_PATH ); strcat( workpath, "\\" ); // printf( "working in: %s\n", workpath ); long hFile; if( (hFile = _findfirst( "*", &c_file )) == -1L ) { // do nothing } else { if( c_file.attrib & _A_SUBDIR ) { if( strlen( c_file.name ) > 2 ) { LPFINDDATA p = new FINDDATA; memcpy( p, &c_file, sizeof(FINDDATA) ); strcpy( p->name, workpath ); strcat( p->name, c_file.name ); stack.push (p); } } else { ProcessFile( &c_file, workpath ); } while( _findnext( hFile, &c_file ) == 0 ) { if( c_file.attrib & _A_SUBDIR ) { if( strlen( c_file.name ) > 2 ) { LPFINDDATA p = new FINDDATA; memcpy( p, &c_file, sizeof(FINDDATA) ); strcpy( p->name, workpath ); strcat( p->name, c_file.name ); stack.push (p); } } else { ProcessFile( &c_file, workpath ); } } _findclose( hFile ); } delete p; } _chdir( oldworkpath ); return; }};#endif // FILE_SEARCH#include "FileSearch.h"class JPGFileSearch : public FileSearch{private:public: JPGFileSearch( char* cSourcePath, char* cDeskPath ):FileSearch(cSourcePath) { } ~JPGFileSearch() { }protected: void ProcessFile( LPFINDDATA p, char* workpath ) { long filenamelen = strlen( p->name ); if( filenamelen > 4) { if( (strcmp( p->name + filenamelen - 4, ".jpg" ) == 0 ) || ( strcmp( p->name + filenamelen - 4, ".jpe" ) ==0 ) ) { long originpathlen = strlen( m_cPath ); char relativepath[_MAX_PATH]; strcpy( relativepath, workpath + originpathlen + 1 ); long relativepathlen = strlen( relativepath ); if( relativepathlen >1 && relativepath[relativepathlen-1] == '\\' ) relativepath[relativepathlen-1] = 0; DealWithFile( p->name, relativepath, workpath ); } } }private: void DealWithFile( char* name, char* relativepath, char* workpath ) { }}; 你模仿JPGFileSearch 自己写一个avisearch吧#ifndef STACK_PP#define STACK_PP#define STACK_SIZE_STEP 1024typedef void* LPVOID;template < class LPTYPE >class StackTempl{private: long m_lGrowthStep; LPTYPE* pptr; long lCurrentSize; long sp; void Init( long lStackGrowthStep ) { pptr = NULL; m_lGrowthStep = lStackGrowthStep; lCurrentSize = m_lGrowthStep; pptr = new LPTYPE[m_lGrowthStep]; sp = 0; }public: StackTempl() { Init(STACK_SIZE_STEP); } StackTempl( long lStackGrowthStep ) { Init( lStackGrowthStep ); } ~StackTempl() { if( pptr != NULL ) { delete []pptr; } }public: void push(LPTYPE p) { if(sp<lCurrentSize) pptr[sp++] = p; else { LPTYPE* pptrTemp = new LPTYPE[lCurrentSize+m_lGrowthStep]; memcpy( pptrTemp, pptr, lCurrentSize*sizeof(LPTYPE) ); delete []pptr; lCurrentSize += m_lGrowthStep; pptr = pptrTemp; pptr[sp++] = p; } } LPTYPE pop(void) { if( sp==0 ) return NULL; else return pptr[--sp]; }};#endif 求MFC+三维建模的效果 在切换视图后,CListCtrl原内容消失 菜菜的问题。。。 按下按钮的时候老提示出错?? 如何以"设计模式"而非"运行模式"创建一个OCX控件,谢谢大家~~~ 请问如何在CTabCtrl上用CtabCtrl,谢谢 帮忙看一下这个程序 内存管理等问题 在mdi中,怎么实现saveall功能? 初学者的100分:怎样设置focus? 帮帮忙,着急阿 请教高手!我用OnFileNew()生成的窗口关闭时出错,为什么?
#define FILE_SEARCH#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <io.h>#include "StackPP.h"typedef _finddata_t FINDDATA;
typedef _finddata_t* LPFINDDATA;
typedef StackTempl < _finddata_t* > FindDataStack;class FileSearch
{protected:
char* m_cPath;protected:
~FileSearch()
{
delete []m_cPath;
m_cPath = NULL;
}
virtual void ProcessFile( LPFINDDATA p, char* workpath ) = 0;public:
FileSearch( char* cPath )
{
m_cPath = NULL;
if( cPath != NULL )
{
long lLength = strlen( cPath );
m_cPath = new char[lLength+1];
strcpy( m_cPath, cPath );
}
}
void Run()
{
char oldworkpath[_MAX_PATH];
_getcwd( oldworkpath, _MAX_PATH ); // ±
#define FILE_SEARCH#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <io.h>#include "StackPP.h"typedef _finddata_t FINDDATA;
typedef _finddata_t* LPFINDDATA;
typedef StackTempl < _finddata_t* > FindDataStack;class FileSearch
{protected:
char* m_cPath;protected:
~FileSearch()
{
delete []m_cPath;
m_cPath = NULL;
}
virtual void ProcessFile( LPFINDDATA p, char* workpath ) = 0;public:
FileSearch( char* cPath )
{
m_cPath = NULL;
if( cPath != NULL )
{
long lLength = strlen( cPath );
m_cPath = new char[lLength+1];
strcpy( m_cPath, cPath );
}
}
void Run()
{
char oldworkpath[_MAX_PATH];
_getcwd( oldworkpath, _MAX_PATH ); // 保存当前工作路径
FindDataStack stack;
FINDDATA c_file;
strcpy( c_file.name, m_cPath );
LPFINDDATA p = new FINDDATA;
memcpy( p, &c_file, sizeof(FINDDATA) );
stack.push (p);
char workpath[_MAX_PATH];
while( (p = stack.pop()) != NULL )
{
_chdir( p->name );
_getcwd( workpath, _MAX_PATH );
strcat( workpath, "\\" );
// printf( "working in: %s\n", workpath );
long hFile;
if( (hFile = _findfirst( "*", &c_file )) == -1L )
{
// do nothing
}
else
{
if( c_file.attrib & _A_SUBDIR )
{
if( strlen( c_file.name ) > 2 )
{
LPFINDDATA p = new FINDDATA;
memcpy( p, &c_file, sizeof(FINDDATA) );
strcpy( p->name, workpath );
strcat( p->name, c_file.name );
stack.push (p);
}
}
else
{
ProcessFile( &c_file, workpath );
}
while( _findnext( hFile, &c_file ) == 0 )
{
if( c_file.attrib & _A_SUBDIR )
{
if( strlen( c_file.name ) > 2 )
{
LPFINDDATA p = new FINDDATA;
memcpy( p, &c_file, sizeof(FINDDATA) );
strcpy( p->name, workpath );
strcat( p->name, c_file.name );
stack.push (p);
}
}
else
{
ProcessFile( &c_file, workpath );
}
}
_findclose( hFile );
}
delete p;
}
_chdir( oldworkpath );
return;
}};#endif // FILE_SEARCH#include "FileSearch.h"
class JPGFileSearch : public FileSearch
{
private:public:
JPGFileSearch( char* cSourcePath, char* cDeskPath ):FileSearch(cSourcePath)
{
}
~JPGFileSearch()
{
}
protected:
void ProcessFile( LPFINDDATA p, char* workpath )
{
long filenamelen = strlen( p->name );
if( filenamelen > 4)
{
if( (strcmp( p->name + filenamelen - 4, ".jpg" ) == 0 ) || ( strcmp( p->name + filenamelen - 4, ".jpe" ) ==0 ) )
{
long originpathlen = strlen( m_cPath );
char relativepath[_MAX_PATH];
strcpy( relativepath, workpath + originpathlen + 1 );
long relativepathlen = strlen( relativepath );
if( relativepathlen >1 && relativepath[relativepathlen-1] == '\\' ) relativepath[relativepathlen-1] = 0;
DealWithFile( p->name, relativepath, workpath );
}
}
}
private: void DealWithFile( char* name, char* relativepath, char* workpath )
{ }
};
#define STACK_PP#define STACK_SIZE_STEP 1024
typedef void* LPVOID;
template < class LPTYPE >
class StackTempl
{
private:
long m_lGrowthStep;
LPTYPE* pptr;
long lCurrentSize;
long sp;
void Init( long lStackGrowthStep )
{
pptr = NULL;
m_lGrowthStep = lStackGrowthStep;
lCurrentSize = m_lGrowthStep;
pptr = new LPTYPE[m_lGrowthStep];
sp = 0;
}public:
StackTempl()
{
Init(STACK_SIZE_STEP);
}
StackTempl( long lStackGrowthStep )
{
Init( lStackGrowthStep );
}
~StackTempl()
{
if( pptr != NULL )
{
delete []pptr;
}
}
public:
void push(LPTYPE p)
{
if(sp<lCurrentSize)
pptr[sp++] = p;
else
{
LPTYPE* pptrTemp = new LPTYPE[lCurrentSize+m_lGrowthStep];
memcpy( pptrTemp, pptr, lCurrentSize*sizeof(LPTYPE) );
delete []pptr;
lCurrentSize += m_lGrowthStep;
pptr = pptrTemp;
pptr[sp++] = p;
}
}
LPTYPE pop(void)
{
if( sp==0 )
return NULL;
else
return pptr[--sp];
}
};#endif