【发布时间】:2014-12-22 21:32:41
【问题描述】:
我想让这个程序在 VS 2013 上运行。放置以下内容无济于事:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#define USE_STANDARD_FILE_FUNCTIONS
我仍然得到:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> Code3C.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\afx.h(38): warning C4996: 'MBCS_Support_Deprecated_In_MFC': MBCS support in MFC is deprecated and may be removed in a future version of MFC.
1> c:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\afx.h(33) : see declaration of 'MBCS_Support_Deprecated_In_MFC'
1> _WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)
1>c:\users\revist\desktop\sallcode\sallcode\code3c\code3c.cpp(76): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\stdio.h(211) : see declaration of 'fopen'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
程序:
CCode3.h:
#include <afxwin.h>
#include <afxdlgs.h> //dialog boxes
#include "resource.h"
#define n 10
class CCode3C : public CFrameWnd
{
private:
CPoint *pt;
public:
CCode3C();
CCode3C::~CCode3C();
afx_msg void OnFileOpen();
afx_msg void OnFileSave();
afx_msg void OnGenerate();
afx_msg void OnClear();
afx_msg void OnExit();
DECLARE_MESSAGE_MAP()
};
class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
CCode3C.cpp
#include "Code3C.h"
CMyWinApp MyApplication;
BOOL CMyWinApp::InitInstance()
{
CCode3C* pFrame = new CCode3C;
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CCode3C,CFrameWnd)
ON_COMMAND(ID_FILEOPEN,OnFileOpen)
ON_COMMAND(ID_FILESAVE,OnFileSave)
ON_COMMAND(ID_GENERATE,OnGenerate)
ON_COMMAND(ID_CLEAR,OnClear)
ON_COMMAND(ID_EXIT,OnExit)
END_MESSAGE_MAP()
CCode3C::CCode3C()
{
Create(NULL, "Code3C: File menus",
WS_OVERLAPPEDWINDOW,CRect(0,0,600,400),
NULL,MAKEINTRESOURCE(IDR_MENU1));
pt=new CPoint [n+1];
}
CCode3C::~CCode3C()
{
delete pt;
}
void CCode3C::OnClear()
{
CClientDC dc(this);
CRect rc;
GetClientRect(&rc);
CBrush whiteBrush(RGB(255,255,255));
dc.FillRect(&rc,&whiteBrush);
for (int i=1;i<=n;i++)
pt[i]=CPoint(0,0);
}
void CCode3C::OnGenerate()
{
CClientDC dc(this);
CString str;
time_t seed=time(NULL);
srand((unsigned)seed);
OnClear();
dc.TextOut(50,50,"Generating Random Numbers");
for (int i=1;i<=n;i++)
{
pt[i].x=100+rand()%400; pt[i].y=50+rand()%300;
str.Format("%d %d",pt[i].x,pt[i].y);
dc.TextOut(50,80+20*i,str);
}
}
void CCode3C::OnFileOpen()
{
CClientDC dc(this);
CString str;
CRect rc;
FILE *ifp;
char strFilter[] = {"TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||"};
CFileDialog FileDlg(TRUE,".txt",NULL,0,strFilter);
if (FileDlg.DoModal()==IDOK)
{
str=FileDlg.GetFileName();
ifp = fopen(str, "r");
dc.TextOut(350,50,"File Opened: "+str);
for (int i=1;i<=n;i++)
{
fscanf(ifp,"%d %d",&pt[i].x,&pt[i].y);
rc=CRect(pt[i].x-30,pt[i].y-30,pt[i].x+30,pt[i].y+30);
dc.Ellipse(rc);
rc=CRect(pt[i].x-1,pt[i].y-1,pt[i].x+1,pt[i].y+1);
dc.Rectangle(rc);
}
fclose(ifp);
}
}
void CCode3C::OnFileSave()
{
CClientDC dc(this);
CString str;
FILE *ofp;
char strFilter[] = {"TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||"};
CFileDialog FileDlg(FALSE,".txt",NULL,0,strFilter);
if( FileDlg.DoModal()==IDOK)
{
str=FileDlg.GetFileName();
ofp=fopen(str,"w");
dc.TextOut(50,20,"File Saved: "+str);
str.Format("%d",n);
dc.TextOut(50,50,"Contents: "+str+" randomly generated numbers");
for (int i=1;i<=n;i++)
fprintf(ofp,"%d %d\n",pt[i].x,pt[i].y);
fclose(ofp);
}
}
void CCode3C::OnExit()
{
CCode3C::OnExit();
}
请记住,我是编程新手(大约 4 周前开始),因此请回答以下问题之一:是的,我是认真的。
【问题讨论】:
-
这是在开玩笑吗?你是什么意思“它没有帮助把......”???准确描述问题是什么......哦,摆脱那些X!
-
void CCode3C::OnExit() { CCode3C::OnExit(); }你是认真的吗? -
是的,我是。此外,这段代码来自一本书,所以我宁愿说作者是认真的。
-
如果您使用的是默认的预编译头文件,则需要将这些定义放在
stdafx.h的顶部。 -
@FrédéricHamidi OP 说 “此代码来自一本书” - 和 it seems it does。不过我也不敢相信。作者将其描述为“退出程序的正式方式” !!!