What is wrong with this simple Windows MFC code ?

L

learningGuy

Can someone tell me what is wrong with this simple code?
I get an exception every time at the myFile.Open() line.
I have included the code that I think is needed to for you to answer this
below:
Thanks!
============
// MBT.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MBT.h"
#include <iostream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>


////////////////////////////////////////////////////////////////////////////
/
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
//cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}


HANDLE hFile = CreateFile(_T("TimerFile.txt"),
GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

CFile myFile;
CFileException e;

// Exception occurs at next line ...
if (myFile.Open("TimerFile.txt", CFile::modeNoTruncate | CFile::typeText |
CFile::modeWrite, &e))
{
cout << "It opened\n";
}
else
cout << "Couldn't open the file ...\n" << endl;

return nRetCode;
}
/* ================== stdafx.h include file ==================== */
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#if !defined(AFX_STDAFX_H__4354F772_1FA2_4242_A8F9_621E97419373__INCLUDED_)
#define AFX_STDAFX_H__4354F772_1FA2_4242_A8F9_621E97419373__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common
Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include <iostream>

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.

#endif //
!defined(AFX_STDAFX_H__4354F772_1FA2_4242_A8F9_621E97419373__INCLUDED_)
 
M

Mike Wahler

learningGuy said:
Can someone tell me what is wrong with this simple code?

Yes. You posted it in the wrong place. Here, we
only discuss ISO standard C++, which is a platform
independent language which has no such features as
'MFC'.

[snip 100 lines of MFC code]

Try newsgroup microsoft.public.vc.mfc

And before posting to comp.lang.c++ again, please read:
http://www.slack.net/~shiva/welcome.txt

Thank you.
-Mike
I get an exception every time at the myFile.Open() line.
I have included the code that I think is needed to for you to answer this
below:
Thanks!
============
// MBT.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MBT.h"
#include <iostream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>


////////////////////////////////////////////////////////////////////////////
/
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
//cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}


HANDLE hFile = CreateFile(_T("TimerFile.txt"),
GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

CFile myFile;
CFileException e;

// Exception occurs at next line ...
if (myFile.Open("TimerFile.txt", CFile::modeNoTruncate | CFile::typeText |
CFile::modeWrite, &e))
{
cout << "It opened\n";
}
else
cout << "Couldn't open the file ...\n" << endl;

return nRetCode;
}
/* ================== stdafx.h include file ==================== */
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#if !defined(AFX_STDAFX_H__4354F772_1FA2_4242_A8F9_621E97419373__INCLUDED_)
#define AFX_STDAFX_H__4354F772_1FA2_4242_A8F9_621E97419373__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common
Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include <iostream>

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.

#endif //
!defined(AFX_STDAFX_H__4354F772_1FA2_4242_A8F9_621E97419373__INCLUDED_)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top