Accessing File Status
CFile also supports getting file status, including whether the file exists, creation and modification dates and times, logical size, and path.
- 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++获取当前路径实现技巧分享
C++获取当前路径的实现方法有很多种,我们在这里搜罗了两个比较常用简单的方法供大家参考。希望能够让大家从中获得一些帮助。
AD:
C++编程语言在实际应用中,可以帮助开发人员轻松的实现各种功能与需求。比如在对文件的操作上就可以灵活的运用各种个方法来实现。那么接下来我们将会给大家介绍一下C++获取当前路径的相关实现方法。
C++获取当前路径相关实现代码示例:
1.以下代码来自CSDN 2004 VC编程经验总结.
- {
- CString strPath;
- GetCurrentDirectory(MAX_PATH,strPath.GetBuffer(MAX_PATH));
- strPath.ReleaseBuffer();
- return strPath;
- }
2.以下代码来自TZ MFC.NET Primer 1.01,
- CString CPropertyGridSampleApp::GetCurrWorkingDir()
- {
- CString strPath;
- TCHAR szFull[_MAX_PATH];
- TCHAR szDrive[_MAX_DRIVE];
- TCHAR szDir[_MAX_DIR];
- ::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));
- _tsplitpath(szFull, szDrive, szDir, NULL, NULL);
- _tcscpy(szFull, szDrive);
- _tcscat(szFull, szDir);
- strPath = CString(szFull);
- 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=引号内字符串或者字符串变量+引号内字符串或者字符串变量.
切记,切记!!!!