VC 文件路径常用操作

41 篇文章 0 订阅

文件路径常用操作


━━━━━━━━━━━━━━━━━━━━━━━━

 

#include <iostream>
#include <windows.h>
#include <TCHAR.H>
using namespace std;

void GetFileNameInPath(const TCHAR* FilePath,TCHAR* FileNameBuf,UINT BufSize);
void GetPathWithoutFileName (TCHAR* FilePath);
void GetPathWithoutFileName (const TCHAR* FilePath,TCHAR* NewFilePath);

void main()
{
 TCHAR path[MAX_PATH];
 _tcscpy(path,_T("D://MyProjects//临时程序//a.txt"));
// _tcscpy(path,_T("D://MyProjects//临时程序"));
// _tcscpy(path,_T("a.txt"));
// _tcscpy(path,_T("D:"));
// _tcscpy(path,_T(""));

 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 //判断文件或文件夹是否存在,最后的//号有无都没关系
 if (-1!=GetFileAttributes(path))
  cout<<path<<_T("  存在")<<endl;
 else
  cout<<path<<_T("  不存在")<<endl;


 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 //判断路径是否一个文件夹
 DWORD rel=GetFileAttributes(path);
 if (rel!=-1 && (NULL!=(FILE_ATTRIBUTE_DIRECTORY&rel)))
  cout<<path<<_T("  是一个文件夹")<<endl;
 else
  cout<<path<<_T("  不是一个文件夹")<<endl;

 
 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 //简单文件扩展名,输入参数path或文件名,输出扩展名NameExtension
 {
  const TCHAR* NameExtension;
  const TCHAR* p=path+_tcslen(path)-1;
  while( *p!='.' &&  p!=path ) p--; 
  if (p==path)NameExtension=path;
  else  NameExtension=p+1;
  cout<<path<<_T("  文件扩展名:")<<NameExtension<<endl;
 }

 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 //简单提取路径中的文件或文件夹名,输入参数path,输出名称FileName
 const TCHAR* FileName;
 const TCHAR* p=path+_tcslen(path)-1;
 while( *p!='//' &&  p!=path ) p--; 
 if (p==path)FileName=path;
 else  FileName=p+1;
 cout<<path<<_T("  提取文件名或文件夹名:")<<FileName<<endl;


 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 {
  TCHAR FileName[100];
  GetFileNameInPath(path,FileName,sizeof(FileName)/sizeof(TCHAR));
  cout<<path<<_T("  提取文件名或文件夹名:")<<FileName<<endl;
 }

 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 TCHAR OldPath[MAX_PATH];
 _tcscpy(OldPath,path);
 GetPathWithoutFileName(OldPath); //参数OldPath必须指向一个可以修改的内存
 cout<<path<<_T("  提取路径:")<<OldPath<<endl;
 
 
 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 TCHAR NewFilePath[MAX_PATH];
 GetPathWithoutFileName(path,NewFilePath);
 cout<<path<<_T("  提取路径:")<<NewFilePath<<endl;
 
 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 {
  TCHAR drive[_MAX_DRIVE];TCHAR dir[_MAX_DIR];TCHAR fname[_MAX_FNAME];TCHAR ext[_MAX_EXT];
  _splitpath((LPTSTR)(LPCTSTR)path, drive, dir, fname, ext );
  cout<<_T("分解路径")<<endl;
  cout<<drive<<_T("   ")<<dir<<_T("   ")<<fname<<_T("   ")<<ext<<endl;
 }

 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 //设置程序运行的目录跟程序文件所在目录相同
 //当我们的程序被其他程序打开时,它的工作目录和打开它的那。
 //个程序的目录是相同的。所以我们需要把目录改回来
 TCHAR AppPath[MAX_PATH];
 int len=GetModuleFileName(NULL,AppPath,MAX_PATH);
 while(AppPath[--len]!='//'); 
 AppPath[len]='/0';
 SetCurrentDirectory(AppPath);
}


/****************************************************************************
提取路径中的文件名
当只有一个文件名时,则直接复制此文件名
但当路径是D:时,返回的文件名也是D:
****************************************************************************/
void GetFileNameInPath(const TCHAR* FilePath,TCHAR* FileNameBuf,UINT BufSize)
{
 //pos初始化为指向最后一个字符,
 //如果搜索到,则pos指向这个字符,否则pos指向字符串开头
 const TCHAR* pos=FilePath+_tcslen(FilePath)-1;
 while( *pos!='//' &&  pos!=FilePath ) pos--; 
 if (pos==FilePath) _tcsncpy(FileNameBuf,pos,BufSize-1);
 else    _tcsncpy(FileNameBuf,pos+1,BufSize-1);
 FileNameBuf[BufSize-1]=NULL;
}


/****************************************************************************
1.截取掉最后一个//后面的文件或文件夹名
2.函数调用后FilePath保存了新的路径
3.参数FilePath必须指向一个可以修改的内存
换言之:如下调用会错误。
GetPathWithoutFileName(_T("c://a.txt"));
****************************************************************************/
void GetPathWithoutFileName (TCHAR* FilePath)
{
 //pos初始化为指向最后一个字符,
 //如果搜索到,则pos指向这个字符,否则pos指向字符串开头
 TCHAR* pos=FilePath+_tcslen(FilePath)-1;
 while( *pos!='//' &&  pos!=FilePath ) pos--;
 
 if (pos!=FilePath)
  *(pos+1)=NULL;//路径保留了最后的/,如果想去掉,_tcscpy(NewFilePath,pos)
}


/****************************************************************************
提取路径
传递的NewFilePath必须创建为MAX_PATH
****************************************************************************/
void GetPathWithoutFileName (const TCHAR* FilePath,TCHAR* NewFilePath)
{
 //pos初始化为指向最后一个字符,
 //如果搜索到,则pos指向这个字符,否则pos指向字符串开头
 const TCHAR* pos=FilePath+_tcslen(FilePath)-1;
 while( *pos!='//' &&  pos!=FilePath ) pos--;
 if (pos==FilePath) _tcscpy(NewFilePath,FilePath);
 else
 {
  int num=pos+1-FilePath; //路径保留了最后的/,如果想去掉,int num=pos-FilePath
  _tcsncpy(NewFilePath,FilePath,num);
  NewFilePath[num]=NULL; 
 }
}

/*
文件名 简介
CopyFileCopyFileEx 复制文件
CreateDirectoryCreateDirectoryEx 创建文件夹
CreateFile 新建或打开文件
DeleteFile 删除文件
FindFirstFileFind  FirstFileExFindNext  FileFindClose 这3组函数配合进行文件的查找
GetCurrentDirectory 取得当前文件夹
GetFileAttributes  GetFileAttributesEx 取得文件属性
GetFileSize 取得文件的大小
GetFileType 取得文件类型
GetFullPathName 函数获取文件的完整路径名,只有当该文件在当前目录下,结果才正确
GetLongPathName 将文件名转化成长文件名
GetShortPathName 将文件名转化成短文件名
GetTempFileName 取得一个临时文件名,以避免和其他临时文件重名
GetTempPath 取得临时文件夹的路径
MoveFileMoveFileEx 移动文件
ReadFileReadFileEx 读文件
RemoveDirectory 删除文件夹
SetCurrentDirectory 设置临时文件
SetFileAttributes 设置文件属性
SetFilePointer 移动文件指针
WriteFileWriteFileEx 写文件
*/


/*
1.对于#include后面双引号里的路径,(中的来说,include 里 的/和//可以互换,文件在本目录下可以省略/)
  #include "jacky.h"    可以
  #include "./jacky.h"  可以
  #include ".//jacky.h"  可以

  #include "../jacky.h"  可以
  #include "..//jacky.h"  可以
 
 
2.对于WritePrivateProfileString
 WritePrivateProfileString("jacky","i",buf,"aa.ini");  错误
 WritePrivateProfileString("jacky","i",buf,"./aa.ini"); 错误
 WritePrivateProfileString("jacky","i",buf,".//aa.ini"); 正确
 WritePrivateProfileString("jacky","i",buf,"..//aa.ini"); 正确
 
3.对于fopen
 FILE *fp=fopen("./rrt.txt","wb+");  错误
 FILE *fp=fopen("rrt.txt","wb+");  正确
 FILE *fp=fopen(".//rrt.txt","wb+"); 正确
 FILE *fp=fopen("..//rrt.txt","wb+"); 正确

*/
 

 

━━━━━━━━━━━━━━━━━━━━━━━━
文件名 简介
CopyFileCopyFileEx 复制文件
CreateDirectoryCreateDirectoryEx 创建文件夹
CreateFile 新建或打开文件
DeleteFile 删除文件
FindFirstFileFind  FirstFileExFindNext  FileFindClose 这3组函数配合进行文件的查找
GetCurrentDirectory 取得当前文件夹
GetFileAttributes  GetFileAttributesEx 取得文件属性
GetFileSize 取得文件的大小
GetFileType 取得文件类型
GetFullPathName 函数获取文件的完整路径名,只有当该文件在当前目录下,结果才正确
GetLongPathName 将文件名转化成长文件名
GetShortPathName 将文件名转化成短文件名
GetTempFileName 取得一个临时文件名,以避免和其他临时文件重名
GetTempPath 取得临时文件夹的路径
MoveFileMoveFileEx 移动文件
ReadFileReadFileEx 读文件
RemoveDirectory 删除文件夹
SetCurrentDirectory 设置临时文件
SetFileAttributes 设置文件属性
SetFilePointer 移动文件指针
WriteFileWriteFileEx 写文件


━━━━━━━━━━━━━━━━━━━━━━━━
/*
1.对于#include后面双引号里的路径,(中的来说,include 里 的/和//可以互换,文件在本目录下可以省略/)
     #include "jacky.h"       可以
     #include "./jacky.h"        可以
     #include ".//jacky.h"        可以

     #include "../jacky.h"        可以
     #include "..//jacky.h"        可以
   
   
2.对于WritePrivateProfileString
    WritePrivateProfileString("jacky","i",buf,"aa.ini");  错误
    WritePrivateProfileString("jacky","i",buf,"./aa.ini"); 错误
    WritePrivateProfileString("jacky","i",buf,".//aa.ini"); 正确
    WritePrivateProfileString("jacky","i",buf,"..//aa.ini"); 正确
   
3.对于fopen
    FILE *fp=fopen("./rrt.txt","wb+");  错误
    FILE *fp=fopen("rrt.txt","wb+");     正确
    FILE *fp=fopen(".//rrt.txt","wb+");    正确
    FILE *fp=fopen("..//rrt.txt","wb+"); 正确

*/

━━━━━━━━━━━━━━━━━━━━━━━━

关于:PathFileExists 和 PathIsDirectory

两个函数的返回值有问题!MSDN写着返回BOOL,实际情况不是这样,请慎用。

Description of "Return Value" is wrong.
The description of "Return Value" is wrong.

The correct wording should read:
Returns FILE_ATTRIBUTE_DIRECTORY if the path is a valid directory, or FALSE otherwise.

FALSE = 0
FILE_ATTRIBUTE_DIRECTORY = 16

History

3/2/2010
Detlev Dalitz
Broken return code
It seems to be either FALSE (0) or a non-zero (0x10) value, if(PathIsDirectory() == TRUE) does not eval to true, surprisingly.
Tested it under w7 just now.

 

转载出处:http://blog.csdn.net/jacky_qiu/archive/2010/12/04/6054656.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值