I wish to be able to open a file in my Win32 application.
The method I am using is retrieving the root path from an edit box e.g. "C:\MyFolder" (this is assigned to strPathToSource
). Then I want to append another string e.g. "\source\Utilities\File.h" and store the concatenated path in a new variable strPathToFile
.
So strPathToFile
should contain "C:\MyFolder\source\Utilities\File.h", which can then be opened using infile.open(strPathToFile)
.
The relevant code is shown below:
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;
}
Where PATH_TO_FILE
is defined as:
const char* PATH_TO_FILE = "\\source\\Utilities\\File.h";
My issue is that it is always logging out "File.h not found". I believe the issue lies with the concatenation e.g.
const char* strPathToFile = char(strPathToSource) + PATH_TO_FILE;
Stepping through I can see the values of strPathToSource
and PATH_TO_FILE
are as they should be, but the concatenated result in strPathToFile
is a NULL value I believe.