fopen在vs2012中无法编译通过

liusuanyatong 2014-02-15 04:57:21
该代码可以在vc6.0和vs2008、vs2010下编译运行,却不能在vs2012下编译通过
#include <iostream>
#include <cstdio>
#include <memory>
using namespace std;
int main(int argc,char* argv[])
{
if (argc!=3)
{
cout<<"程序使用方法:C3 /r 源文件"<<endl;
return 0;
}
FILE* pFile;
pFile=fopen(argv[2],"r");
if (NULL==pFile)
{
cout<<"打开指定文件失败,请确定指定文件是否存在!\n";
return 0;
}
if (strcmp(argv[1],"/r")==0)
{
char str[100];
memset(str,0,100);
fread(str,1,100,pFile);
cout<<"读取的数据是:"<<str<<endl;
return 1;
}
}

错误:

1>e:\project\consoleapplication2\consoleapplication2\源.cpp(14): 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> d:\program files (x86)\microsoft visual studio 11.0\vc\include\stdio.h(218) : 参见“fopen”的声明
还请各位指教,应该怎么修改!
...全文
458 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liusuanyatong 2014-02-17
  • 打赏
  • 举报
回复
引用 8 楼 zhuobattle 的回复:
引用 7 楼 liusuanyatong 的回复:
[quote=引用 6 楼 zhuobattle 的回复:] 或者你改下编译器编译选项,把warning提高成error关掉
请问能贴图吗?我修改了,还是要报错的
如果treat as warning改成no也没用的话,那说明vs2012确实把之前这类warning(vs2010之前的)提示成了Error,如果你不想改代码,那么还有一个方法,在工程属性->C/C++ ->Preprocessor的Preprocessor Definitions处添加上面编译提示的那个宏 _CRT_SECURE_NO_WARNINGS;如下图: [/quote]非常感谢,不过分数已经送出去了哈,不好意思哈
zhuobattle 2014-02-15
  • 打赏
  • 举报
回复
引用 7 楼 liusuanyatong 的回复:
引用 6 楼 zhuobattle 的回复:
或者你改下编译器编译选项,把warning提高成error关掉
请问能贴图吗?我修改了,还是要报错的

如果treat as warning改成no也没用的话,那说明vs2012确实把之前这类warning(vs2010之前的)提示成了Error,如果你不想改代码,那么还有一个方法,在工程属性->C/C++ ->Preprocessor的Preprocessor Definitions处添加上面编译提示的那个宏 _CRT_SECURE_NO_WARNINGS;如下图:
liusuanyatong 2014-02-15
  • 打赏
  • 举报
回复
引用 6 楼 zhuobattle 的回复:
或者你改下编译器编译选项,把warning提高成error关掉
请问能贴图吗?我修改了,还是要报错的
zhuobattle 2014-02-15
  • 打赏
  • 举报
回复
或者你改下编译器编译选项,把warning提高成error关掉
mujiok2003 2014-02-15
  • 打赏
  • 举报
回复
引用 4 楼 liusuanyatong 的回复:
[quote=引用 3 楼 mujiok2003 的回复:]

#define _CRT_SECURE_NO_WARNINGS //加上这个
#include <iostream>
#include <cstdio>
#include <memory>
using namespace std;
int main(int argc,char* argv[])
{
	if (argc!=3)
	{
		cout<<"程序使用方法:C3 /r 源文件"<<endl;
		return 0;
	}
	FILE* pFile;
	pFile=fopen(argv[2],"r");
	if (NULL==pFile)
	{
		cout<<"打开指定文件失败,请确定指定文件是否存在!\n";
		return 0;
	}
	if (strcmp(argv[1],"/r")==0)
	{
		char str[100];
		memset(str,0,100);
		fread(str,1,100,pFile);
		cout<<"读取的数据是:"<<str<<endl;
		return 1;
	}
}
引用 3 楼 mujiok2003 的回复:

#define _CRT_SECURE_NO_WARNINGS //加上这个
#include <iostream>
#include <cstdio>
#include <memory>
using namespace std;
int main(int argc,char* argv[])
{
	if (argc!=3)
	{
		cout<<"程序使用方法:C3 /r 源文件"<<endl;
		return 0;
	}
	FILE* pFile;
	pFile=fopen(argv[2],"r");
	if (NULL==pFile)
	{
		cout<<"打开指定文件失败,请确定指定文件是否存在!\n";
		return 0;
	}
	if (strcmp(argv[1],"/r")==0)
	{
		char str[100];
		memset(str,0,100);
		fread(str,1,100,pFile);
		cout<<"读取的数据是:"<<str<<endl;
		return 1;
	}
}
请问为什么加上这个宏之后就可以通过编译了呢?还请赐教![/quote] 这个宏就是为了关闭那个警告/错误的
liusuanyatong 2014-02-15
  • 打赏
  • 举报
回复
引用 3 楼 mujiok2003 的回复:

#define _CRT_SECURE_NO_WARNINGS //加上这个
#include <iostream>
#include <cstdio>
#include <memory>
using namespace std;
int main(int argc,char* argv[])
{
	if (argc!=3)
	{
		cout<<"程序使用方法:C3 /r 源文件"<<endl;
		return 0;
	}
	FILE* pFile;
	pFile=fopen(argv[2],"r");
	if (NULL==pFile)
	{
		cout<<"打开指定文件失败,请确定指定文件是否存在!\n";
		return 0;
	}
	if (strcmp(argv[1],"/r")==0)
	{
		char str[100];
		memset(str,0,100);
		fread(str,1,100,pFile);
		cout<<"读取的数据是:"<<str<<endl;
		return 1;
	}
}
引用 3 楼 mujiok2003 的回复:

#define _CRT_SECURE_NO_WARNINGS //加上这个
#include <iostream>
#include <cstdio>
#include <memory>
using namespace std;
int main(int argc,char* argv[])
{
	if (argc!=3)
	{
		cout<<"程序使用方法:C3 /r 源文件"<<endl;
		return 0;
	}
	FILE* pFile;
	pFile=fopen(argv[2],"r");
	if (NULL==pFile)
	{
		cout<<"打开指定文件失败,请确定指定文件是否存在!\n";
		return 0;
	}
	if (strcmp(argv[1],"/r")==0)
	{
		char str[100];
		memset(str,0,100);
		fread(str,1,100,pFile);
		cout<<"读取的数据是:"<<str<<endl;
		return 1;
	}
}
请问为什么加上这个宏之后就可以通过编译了呢?还请赐教!
mujiok2003 2014-02-15
  • 打赏
  • 举报
回复

#define _CRT_SECURE_NO_WARNINGS //加上这个
#include <iostream>
#include <cstdio>
#include <memory>
using namespace std;
int main(int argc,char* argv[])
{
	if (argc!=3)
	{
		cout<<"程序使用方法:C3 /r 源文件"<<endl;
		return 0;
	}
	FILE* pFile;
	pFile=fopen(argv[2],"r");
	if (NULL==pFile)
	{
		cout<<"打开指定文件失败,请确定指定文件是否存在!\n";
		return 0;
	}
	if (strcmp(argv[1],"/r")==0)
	{
		char str[100];
		memset(str,0,100);
		fread(str,1,100,pFile);
		cout<<"读取的数据是:"<<str<<endl;
		return 1;
	}
}
liusuanyatong 2014-02-15
  • 打赏
  • 举报
回复
引用 1 楼 zmlovelx 的回复:
Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 提示讲的很清楚的。 要么换函数,要么加宏。
使用fopen_s这个函数能行的,但是我不想换函数,也不想用宏,这个难道是编译器的问题,无解了?
帅得不敢出门 2014-02-15
  • 打赏
  • 举报
回复
Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 提示讲的很清楚的。 要么换函数,要么加宏。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧