473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

What is wrong with this simple Windows MFC code ?

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_621 E97419373__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_621 E97419373__INCLUDED_)
Jul 19 '05 #1
1 5948
"learningGuy" <Pl****@Reply-Via-Newsgroup-Only.com> wrote in message
news:WQ****************@newsread2.news.atl.earthli nk.net...
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_621 E97419373__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_621 E97419373__INCLUDED_)

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
5
by: Franco, Gustavo | last post by:
Hi, I have a question, and please I need a answer. How can I finalize a thread running with Application.Run (I need the message loop!!!) without call Thread.Abort?. I want to call...
0
by: Miquel | last post by:
Hi all. I felt frustrated when developing an 'UserControl' derivated from textBox, because sequence event (and Validate event) seems to fail. I Always thought my code was wrong. But after...
8
by: Droopy | last post by:
I am making a managed C++ wrapper for calling legacy C++ code from C#. I don't understand what's wrong with following code : __gc public class SerialBuffer { private: unsigned...
2
by: Oberon | last post by:
Why does this not work as it should? I expect index.aspx to show:. Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\ChatSimple\db\chatusers.mdb Instead, I get: ...
13
by: Kyle Adams | last post by:
I don't know where is the right place to ask this so I will start here. Can someone explain to me what these represent? I think they all have to do with the middleware level, but I really don't...
9
by: Ryan Liu | last post by:
Hi, I use C# wrote an Client/Server application. In production environment, will be 130 clients (Windows XP) connect to a Server (Windows 2000/2003 Server) thought TCP/IP socket in a local 100M...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.