现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

文件路径常用操作

2012-08-21 05:29 工业·编程 ⁄ 共 5217字 ⁄ 字号 暂无评论

粘贴以下代码到一个控制台程序,运行!

#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+"); 正确

*/

给我留言

留言无头像?