CFILE C++获取当前路径 CSting 拼接

来源:互联网 发布:地理学软件 编辑:程序博客网 时间:2024/05/17 02:09

Accessing File Status

Home |  Overview |  How Do I

CFile also supports getting file status, including whether the file exists, creation and modification dates and times, logical size, and path.

To get file status

  • Use the CFile class to get and set information about a file. One useful application is to use theCFile static member function GetStatus to determine if a file exists. GetStatus returns 0 if the specified file does not exist.

Thus, you could use the result of GetStatus to determine whether to use the CFile::modeCreateflag when opening a file, as shown by the following example:

CFile theFile; char* szFileName = "c:\\test\\myfile.dat"; BOOL bOpenOK;  CFileStatus status; if( CFile::GetStatus( szFileName, status ) ) {     // Open the file without the Create flag     bOpenOK = theFile.Open( szFileName,                      CFile::modeWrite ); } else {     // Open the file with the Create flag     bOpenOK = theFile.Open( szFileName,                      CFile::modeCreate | CFile::modeWrite ); } 

For related information


如果要是清空缓存 用

flush()即可


C++获取当前路径实现技巧分享

2010-02-04 11:38 佚名 博客园 我要评论(0) 字号:T | T
一键收藏,随时查看,分享好友!

C++获取当前路径的实现方法有很多种,我们在这里搜罗了两个比较常用简单的方法供大家参考。希望能够让大家从中获得一些帮助。

AD:

C++编程语言在实际应用中,可以帮助开发人员轻松的实现各种功能与需求。比如在对文件的操作上就可以灵活的运用各种个方法来实现。那么接下来我们将会给大家介绍一下C++获取当前路径的相关实现方法。

C++获取当前路径相关实现代码示例:

1.以下代码来自CSDN 2004 VC编程经验总结.

  1. {   
  2. CString strPath;   
  3. GetCurrentDirectory(MAX_PATH,strPath.GetBuffer(MAX_PATH));   
  4. strPath.ReleaseBuffer();   
  5. return strPath;   

2.以下代码来自TZ MFC.NET Primer 1.01,

  1. CString CPropertyGridSampleApp::GetCurrWorkingDir()   
  2. {   
  3. CString strPath;   
  4. TCHAR szFull[_MAX_PATH];   
  5. TCHAR szDrive[_MAX_DRIVE];   
  6. TCHAR szDir[_MAX_DIR];   
  7. ::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));   
  8. _tsplitpath(szFull, szDrive, szDir, NULL, NULL);   
  9. _tcscpy(szFull, szDrive);   
  10. _tcscat(szFull, szDir);   
  11. strPath = CString(szFull);   
  12. return strPath;   

使用第一段C++获取当前路径的代码可以获得应用程序运行时所在目录。但由于在使用Microsoft Visual Studio.NET编译并运行项目时,真正的宿主是IDE,所以当前目录是项目所在目录,并不是DEBUG或者RELEASE目录,这一点需要开发人员注意。而第二段代码可以动态解决此问题。并且增加了Unicode支持(TCHAR)。


CString sql= "select * from Str where SaveStr = '";

sql+=szPayerName;

sql+="' order by  SaveStr"; 

只可以使用sql+=引号内字符串或者字符串变量。

不可以使用sql=引号内字符串或者字符串变量+引号内字符串或者字符串变量.

切记,切记!!!!