收集的一些VC常用函数,陆续加入

本文介绍了几种实用的文件和路径处理方法,包括获取当前路径、文件复制、获取指定目录下的文件名及其标题等。通过这些代码片段,读者可以了解如何在C/C++中进行基本的文件管理和路径操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取当前路径

bool GetCurrentDir( char * fileName )
{

 
char filename[512];

 GetModuleFileName( NULL , filename , 
512);

 (strrchr(filename,
'\'))[1= 0 ;

 filename[strlen(filename)
-1= 0;

 strcpy( fileName , filename );
 
 
return 0 ;
}

文件拷贝

int FileCopy
const char *src,
 
const char *dst )
{
#define BUFSZ 16000
 
 
char            *buf;
 FILE            
*fi;
 FILE            
*fo;
 unsigned        amount;
 unsigned        written;
 
int             result;
 
 buf 
= new char[BUFSZ];
 
 fi 
= fopen( src, "rb" );
 fo 
= fopen( dst, "wb" );
 
 result 
= COPY_OK;
 
if  ((fi == NULL) || (fo == NULL) )
 {
  result 
= COPY_ERROR;
  
if (fi != NULL) fclose(fi);
  
if (fo != NULL) fclose(fo);
 }
 
 
if (result == COPY_OK)
 {
  
do
  {
   amount 
= fread( buf, sizeof(char), BUFSZ, fi );
   
if (amount)
   {
    written 
= fwrite( buf, sizeof(char), amount, fo );
    
if (written != amount)
    {
     result 
= COPY_ERROR; // out of disk space or some other disk err?
    }
   }
  } 
// when amount read is < BUFSZ, copy is done
  while ((result == COPY_OK) && (amount == BUFSZ));
  fclose(fi);
  fclose(fo);
 }
 delete [] buf;
 
return(result);

获取指定目录下的文件名:

void GetFilesOfPath(  char * path , CStringArray &straPathes ){

 HANDLE   hFindFile;   
 WIN32_FIND_DATA   FindData;   
 CString   strPath;     

 straPathes.SetSize(
0,10);  

 
char m_pLayerFilePath[MAX_PATH];
 
char FileToFindPatten[MAX_PATH];

 strcpy( m_pLayerFilePath , path );
 strcat( m_pLayerFilePath , 
"\" );
 
//strcat( m_pLayerFilePath , "*.TAB" );
 strcpy( FileToFindPatten , m_pLayerFilePath );
 strcat( FileToFindPatten , 
"*.TAB" ) ;
 
 hFindFile 
= FindFirstFile(FileToFindPatten,&FindData);   
 
if(hFindFile == INVALID_HANDLE_VALUE)   
  
return;   
 
if(FindData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)   
 {   
  strPath.Format(
"%s%s", m_pLayerFilePath , FindData.cFileName);  
  straPathes.Add(strPath);
 }   
 
while(FindNextFile(hFindFile,&FindData))   
 {   
  
if(FindData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)   
  {   
   strPath.Format(
"%s%s", m_pLayerFilePath , FindData.cFileName);   
   straPathes.Add(strPath);   
  }   
 }  
 FindClose(hFindFile);   
 
return
}

获取指定目录下的文件名(不含路径)

void GetFileTitlesOfPath( char * path ,  CStringArray &straPathes)
{

 HANDLE   hFindFile;   
 WIN32_FIND_DATA   FindData;   
 CString   strPath;     

 straPathes.SetSize(
0,10);  

 
char m_pLayerFilePath[MAX_PATH];
 
char FileToFindPatten[MAX_PATH];

 strcpy( m_pLayerFilePath , path );
 strcat( m_pLayerFilePath , 
"\" );
 
//strcat( m_pLayerFilePath , "*.TAB" );
 strcpy( FileToFindPatten , m_pLayerFilePath );
 strcat( FileToFindPatten , 
"*.TAB" ) ;
 
 hFindFile 
= FindFirstFile(FileToFindPatten,&FindData);   
 
if(hFindFile == INVALID_HANDLE_VALUE)   
  
return;   
 
if(FindData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)   
 {   
  strPath.Format(
"%s%s", m_pLayerFilePath , FindData.cFileName);  
  strPath 
= GetFileTitleFromFileName(strPath,FALSE);
  straPathes.Add(strPath);
 }   
 
while(FindNextFile(hFindFile,&FindData))   
 {   
  
if(FindData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)   
  {   
   strPath.Format(
"%s%s", m_pLayerFilePath , FindData.cFileName);
   strPath 
= GetFileTitleFromFileName(strPath,FALSE);
   straPathes.Add(strPath);   
  }   
 }  
 FindClose(hFindFile);   
 
return;  
}

 

CString GetFileTitleFromFileName(CString FileName, BOOL Ext)   
{   
    
int Where;   
    Where 
= FileName.ReverseFind('\');  
    
if (Where == -1)  
        Where 
= FileName.ReverseFind('/');  
    CString FileTitle 
= FileName.Right(FileName.GetLength() - 1 - Where);  
    
if (!Ext)  
    {  
        
int Which = FileTitle.ReverseFind('.');   
        
if (Which != -1)   
            FileTitle 
= FileTitle.Left(Which);   
    }   
    
return FileTitle;
}  

 

CString GetFilePathFromFileName(CString FileName)   
{   
    
int Where;   
    Where 
= FileName.ReverseFind('\');  
    
if (Where == -1)  
        Where 
= FileName.ReverseFind('/');  
    CString FilePath 
= FileName.Left(Where);  
    
return FilePath + '\';   
}


//判断是否为数字或字母
//Output:
//return false,true
bool IsAlphaNumeric(char* charp,int len){
 char *c;
 c=charp;
 for(int i=0;i<len;i++){
  if(*c==NULL || *c=='\0')
   return true;
  if(isalnum(*c)==0)
   return false;
  c++;
 }
 return true;
}

判断是否为数字
bool IsNumeric(char* charp,int len){
    
char *c;
    c
=charp;
    
for(int i=0;i<len;i++){
        
//printf("%d",isdigit(*c));
        if(*c==NULL || *c=='
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值