在VS2013编译器里,在debug或release模式下
//#include "stdlib.h"
//#include "math.h"
#include
#include
using namespace std;
#define PI 3.14159//圆周率宏定义
#define LENGTH_NAME_BMP 30//bmp图片文件名的最大长度
errno_t err;
void main()
{
char Filename[LENGTH_NAME_BMP];
cout << "请输入所需要读取的文件名" << endl;
cin >> Filename;
FILE *fpi=NULL, *fpw;
//cout << fpi << endl;
err = fopen_s(&fpi, Filename,"rb");
/*int i = err;
cout << i << endl;
cout << fpi << endl;*/
if (err == 0)
{
unsigned short filetype;
fread(&filetype, 1, sizeof(unsigned short), fpi);
if (filetype != 0x4d42)
{
cout << "文件不是BMP类型" << endl;
return ;
}
}
else
{
cout << "打开文件失败,错误代码:" << err << endl;
}
system("pause");
}
err = fopen_s(&fpi, Filename,"rb");
err的值都非0,也就是函数运行失败。
而在debug或release的文件里的 exe里运行 就能运行成功(err的值为0,fpi也不是空指针)
这是文件路径的原因。读的文件跟exe在一个目录。所以debug,release都可以。而调试的时候。当前目录是工程文件目录。所以打开文件失败了。最好用exe路径再拼接文件路径来open打开
将你的图片放在项目文件夹中,因为无论debug还是release,项目文件夹都是在exe目录的上两层,所以可以
cin >> Filename;
char buffer[255] = "..\\..\\";
strcat(buffer, filename);
//下面用buffer作为文件名
修改下
strcat,在vs2013中庸strcat_s
strcat_s(buffer, 255, filename);