我希望能够在我的 Win32 应用程序中打开一个文件。我正在使用的方法是从编辑框中检索根路径,例如“C:\MyFolder”(分配给strPathToSource
)。然后我想附加另一个字符串,例如 "\source\Utilities\File.h" 并将连接的路径存储在一个新变量strPathToFile
中。
所以strPathToFile
应该包含“C:\MyFolder\source\Utilities\File.h”,然后可以使用infile.open(strPathToFile)
.
相关代码如下所示:
ifstream infile;
int bufSize = 1024;
LPTSTR strPathToSource = new TCHAR[bufSize];
GetDlgItemText(hWnd, IDC_MAIN_EDIT_FILEPATH, strPathToSource, bufSize); // Get text from edit box and assign to strPathToSource
const char* strPathToFile = char(strPathToSource) + PATH_TO_FILE;
infile.open(strPathToFile);
if(!infile)
{
log(hWnd, "File.h not found.");
return false;
}
其中PATH_TO_FILE
定义为:
const char* PATH_TO_FILE = "\\source\\Utilities\\File.h";
我的问题是它总是注销“File.h not found”。我认为问题在于连接,例如
const char* strPathToFile = char(strPathToSource) + PATH_TO_FILE;
逐步浏览我可以看到 和 的值strPathToSource
应该PATH_TO_FILE
是,但strPathToFile
我相信的连接结果是 NULL 值。