Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below is a program I use for opening files.
C++
bool GetfilePath(HWND hWnd, wchar_t* pszFilename, const wchar_t* pwstFileTypes)
{
	OPENFILENAME ofn;
	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hWnd;
	ofn.lpstrFile = pszFilename;
	ofn.lpstrFile[0] = L'\0';
	ofn.nMaxFile = 2048;
	ofn.lpstrFilter = pwstFileTypes;
	ofn.nFileOffset = 1;
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = nullptr;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = nullptr;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

	bool bResult = GetOpenFileName(&ofn);

	return bResult;
}
I am able to get the names of images files with it, but when I attempted to get the name of a NotePad file with it it failed to get the name of the file. NotePad is a .txt file.

The other relevant part of the program are shown below.
C++
case  IDC_BUTTON1:
        {
            wchar_t szFilename[2048];
            if (GetfilePath(hDlg, szFilename, pszImageTypes))
            {
                SetDlgItemText(hDlg, IDC_EDIT4, szFilename);

                HDC hdc = GetDC(hDlg);

                HBRUSH hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
                FillRect(hdc, &rc, hBrush);
                DrawAppImage(hdc, rc.left, rc.top, iWidth, iHeight, szFilename);

                ReleaseDC(hDlg, hdc);
            }
        }
        return (INT_PTR)TRUE;

C++
#include "stdAfx.h"

const wchar_t* pszTextType = L"Text Files\0*.txt\0\0";
const wchar_t* pszImageTypes = L"Image Files\0*.jpg;*.jpeg;*.gif;*.png;*.txt;*.bmp\0\0";


What I have tried:

I have spent good time trying to debug.
Posted
Updated 25-Mar-24 22:17pm
v6
Comments
Pete O'Hanlon 11-Mar-24 1:32am    
You don't show the code that actually opens the file here. What does that code look like?
Richard MacCutchan 11-Mar-24 5:22am    
You cannot load a text file into an image object, you need to read it into a character or string array.

Assuming you are asking the system to open the file, you need to check the extension of the file, and then check the system file associations to work out if it is registered. If the file name is indeed "myfile.notepad" (as your subject line says) then there will not be a default application registered to handle it and the attempt will fail - you will see nothing happening and need to check the process return code to spot it.

Similarly, if the path to the file is wrong then the process will fail, and will give you a different return code to tell you that, even if the association exists.

Use the debugger to breakpoint your code at the "open file" system call and check the file name and path carefully, then check the process return code when it has completed. We can't do that for you: we don't have any access to your code while it is running, and no access at all to your file system!
 
Share this answer
 
Comments
Gbenbam 27-Mar-24 14:00pm    
You've been a sour




You've been a very great source of help to me.I value you so much. In fact, ,I owe you a great debit of gratitude for your various assistance in the past . But, in spite of the fact that this your solution educates one on the process of debugging, this your solution is completely irrelevant to my question.

I take it that you have never used GetOpenFilename before.
To load a text file I would expect this code:
C
wchar_t szFilename[2048];
const wchar_t* pszTextType = L"Text Files\0*.txt\0\0";
bool rtn = GetfilePath(hDlg, szFilename, pszTextType);

The question then arises as to what is in the file and how the content should be processed or displayed.
In the example, this function is called with the file path:
C
DrawAppImage(hdc, rc.left, rc.top, iWidth, iHeight, szFilename);

What the subroutine looks like exactly has not been said, but from the name it does not sound like text files make sense.
 
Share this answer
 
Comments
Gbenbam 25-Mar-24 13:02pm    
This is the only relevant effort to this question. I tried to edit the question to introduce clarity on my smartphone but all my efforts were wasted when the initial question was reloaded on update.


As to your question. This is part of an application that ( among other things) renders images on Windows.
Users have to fill forms ( typical win32 controls forms). For users who have a large amount of data to submit, I have developed a format for presenting the data in a .txt file and submitting to the software administrator by either Whatsapp, Facebook Messenger or email. The file path will be gotten through this code. There is another program that parses the text file to check for conformity with spelled out format and reports errors on finding such non conformity. If the 'data-feeding-in' file content conforms to specified format. The program will harvest the data in it and populate data base with the data in it.

Getting the filename is the trivial part, parsing, error reporting to users or populating database with harvested data is the more important part. I wanted to debug and test the data-harvesting-program, but could not because I could not get the name of the file ( whose content I created for the purpose of testing and debugging the data harvesting program). It just happens that the code gets images path but would not get .txt file path.

All that the function DrawAppImage does is to render the image on the window via the device context.

I use the wrapper GetFilePath for all my filename getting task, then pass the filename to an appropriate program for further processing.

In the above example, further processing means rendering the image file on the window. In the case of the text file, it would mean parsing the file and populating database with harvested data if the parse is successful. Can you help look into while my specified file filter for text files failed?
Given what you have posted, you are only attempting to choose image types in your file dialog.
Text Files\0*.txt\0Image Files\0*.jpg;*.jpeg;*.gif;*.png;*.bmp\0All files (*.*)\0*.*\0\0
Just pass that string into your GetfilePath method and you should be able to open up .txt files.
 
Share this answer
 
Comments
Gbenbam 25-Mar-24 13:11pm    
Okay, I see you have given me a different string that allow selection of all files. Kindly ignore my last comment. I
Posted it without actually understanding your solution. I think I will delete it instead so as not to confuse others. Don't bother if you don't see it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900