Click here to Skip to main content
12,999,760 members (43,957 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 23 Jan 2012

CFileDialog in a Console Application

, 23 Jan 2012
Rate this:
Please Sign up or sign in to vote.
CFileDialog in a Console Application


How to add a CFileDialog, to a Console application.


Sometimes, you would like to Open a text file without the need of an argument for the EXE.

Using the Code

Just add the following code and add the appropriate include files.

In your stdafx.h file.

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
#pragma once
#define _AFXDLL
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <cctype>
#include <conio.h>
#include <iomanip>
#include <afxdlgs.h>
#include <atlstr.h>

The main project file.
// Any source code blocks look like this:
#include "stdafx.h"
#include "console.h"
using namespace std;
	CString szFileName = "";
	CString szFilter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*";
	char ch;
	cout << "Open File 'y' or 'Y' ? ";
	cin >> ch;
	if((ch == 'y') || (ch == 'Y'))
		CWnd* pWnd = CWnd::FromHandle(GetForegroundWindow()); 
		CFileDialog dlg(TRUE, _T("*.txt"), _T("*.txt"), NULL, szFilter, pWnd);
		dlg.m_ofn.Flags |= OFN_FILEMUSTEXIST;
		dlg.m_ofn.lpstrTitle = _T("Read a text file");
		if(dlg.DoModal() == IDOK)
			szFileName = dlg.GetPathName();
		szFileName = "ReadMe.txt";
	ifstream in(szFileName, ios::in | ios::binary);
	if(!in) {
		cout << "Cannot open input file.\n";
		cout << "Press any key to Exit!";
		return 1;


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 23 Jan 2012
Article Copyright 2012 by Roger65
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid