filepath.cpp

来自「语法检查程序」· C++ 代码 · 共 44 行

CPP
44
字号
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomon and Yonat Sharon, and is   */
/* bound by the MIT open source license.                                   */ 
/* See License.txt or visit www.opensource.org/licenses/mit-license.html   */
/***************************************************************************/

#include "stdafx.h"
#include "FilePath.h"
#include <io.h>

CString GetFilePath(const CString& fileName)
{

	// try in the application directory
	TCHAR path[_MAX_PATH];
	GetModuleFileName(AfxGetInstanceHandle(), path, _MAX_PATH);
	for ( size_t end = _tcslen(path)-1;
		  end > 0 && path[end] != '\\';
		  --end )
		;
	_tcscpy(path+end+1, fileName);
	if (_taccess(path, 0) == 0)
		return path;

	// try in Windows directory
	TCHAR path2[_MAX_PATH];
	_tcscpy(path2, _tgetenv("windir"));
	size_t len = _tcslen(path2);
	path2[len] = '\\';
	_tcscpy(path2+len+1, fileName);
	if (_taccess(path2, 0) == 0)
		return path2;

    FILE* f = fopen(path, "w+");
    if (f) {
        fflush(f);
        fclose(f);
        return path;
    }

    return "";
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?