来自 dev-c++ 的代码不适用于 visual studio 2013
Code from dev-c++ don't work on visual studio 2013
我正在用 C++ 和 SFML 编写一个小游戏,但我一直坚持生成一个准确的迷宫,所以我在互联网上找到了一些代码并根据我的需要进行了调整,并创建了一个迷宫到 .bmp 文件。现在我想在程序中读取它并更改它以适合我的地图并保存到名为 level.map 的文件中。在位图中 reader 我基于来自互联网的一些代码并且它运行良好,但仅适用于 dev-c++ 5.11(64 位和 32 位版本)但是当我在 visual studio 2013 上编译它时,它到处读取 0 值,而不是混合 1 和 0。
我的代码:
#include <conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
struct FileHeader {
short bfType;
int bfSize;
short bfReserved1;
short bfReserved2;
short bfOffBits;
};
FileHeader File;
struct PictureHeader {
int biSize;
int biWidth;
int biHeight;
short biPlanes;
short biBitCount;
int biCompression;
int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
int biClrUsed;
int biClrImportant;
};
PictureHeader Picture;
struct RGBcolors {
char R;
char G;
char B;
};
RGBcolors Rgb;
char dane;
string str;
string bottom = "111111111111111111111111111111111";
int main(int argc, char*argv[]) {
FILE *f = fopen("Maze.bmp", "rb");
system("cls");
printf("\n Info about BITMAP\n\n");
fread(&File.bfType, sizeof(File.bfType), 1, f);
printf(" Typ: %x", File.bfType);
fread(&File.bfSize, sizeof(File.bfSize), 1, f);
printf("\n Size of file: %d bytes", File.bfSize);
fread(&File.bfReserved1, sizeof(File.bfReserved1), 1, f);
printf("\n Reserved2: %d", File.bfReserved1);
fread(&File.bfReserved2, sizeof(File.bfReserved2), 1, f);
printf("\n Reserved2: %d", File.bfReserved2);
fread(&File.bfOffBits, sizeof(File.bfOffBits), 1, f);
printf("\n");
fseek(f, 14, SEEK_SET);
fread(&Picture.biSize, sizeof(Picture.biSize), 1, f);
printf("\n Size of header: %d", Picture.biSize);
fread(&Picture.biWidth, sizeof(Picture.biWidth), 1, f);
printf("\n Width: %d px", Picture.biWidth);
fread(&Picture.biHeight, sizeof(Picture.biHeight), 1, f);
printf("\n Heigh: %d px", Picture.biHeight);
fread(&Picture.biPlanes, sizeof(Picture.biPlanes), 1, f);
fread(&Picture.biBitCount, sizeof(Picture.biBitCount), 1, f);
printf("\n Bits per pixel: %d (1, 4, 8, or 24)", Picture.biBitCount);
fread(&Picture.biCompression, sizeof(Picture.biCompression), 1, f);
printf("\n Compression: %d (0=none, 1=RLE-8, 2=RLE-4)", Picture.biCompression);
fread(&Picture.biSizeImage, sizeof(Picture.biSizeImage), 1, f);
printf("\n Size of only a image: %d", Picture.biSizeImage);
fread(&Picture.biXPelsPerMeter, sizeof(Picture.biXPelsPerMeter), 1, f);
fread(&Picture.biYPelsPerMeter, sizeof(Picture.biYPelsPerMeter), 1, f);
fread(&Picture.biClrUsed, sizeof(Picture.biClrUsed), 1, f);
fread(&Picture.biClrImportant, sizeof(Picture.biClrImportant), 1, f);
fseek(f, File.bfOffBits, SEEK_SET);
cout << "\n\n\n Structure:\n";
int bmpImg;
int c = 0;
for (int i = File.bfOffBits; i < File.bfSize; i++)
{
fseek(f, i, SEEK_SET);
fread(&bmpImg, 1, 1, f);
if ((i - File.bfOffBits) % 96 == 0)
{
cout << endl;
if (i != File.bfOffBits)
str += "1\n";
c++;
}
if ((i - File.bfOffBits) % 3 == 0) {
printf("%d", (bmpImg == 255 ? 1 : 0));
str += (bmpImg == 255 ? "1" : "0");
}
}
printf("\n");
fclose(f);
str += "1\n";
str += bottom;
{
}
if (str.length() > 1)
{
FILE *plik = fopen("maze.map", "w");
for (int i = 0; i < str.length(); i++)
{
fprintf(plik, "%c", str[i]);
}
fclose(plik);
}
return 0;
}
位图大小为 32x32。
例如。
dev-c++ 的正确结果
visual studio 结果不正确:
感谢任何建议和指导
已解决
刚刚用我从 fread 读取到的任何值变量初始化。
我正在用 C++ 和 SFML 编写一个小游戏,但我一直坚持生成一个准确的迷宫,所以我在互联网上找到了一些代码并根据我的需要进行了调整,并创建了一个迷宫到 .bmp 文件。现在我想在程序中读取它并更改它以适合我的地图并保存到名为 level.map 的文件中。在位图中 reader 我基于来自互联网的一些代码并且它运行良好,但仅适用于 dev-c++ 5.11(64 位和 32 位版本)但是当我在 visual studio 2013 上编译它时,它到处读取 0 值,而不是混合 1 和 0。
我的代码:
#include <conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
struct FileHeader {
short bfType;
int bfSize;
short bfReserved1;
short bfReserved2;
short bfOffBits;
};
FileHeader File;
struct PictureHeader {
int biSize;
int biWidth;
int biHeight;
short biPlanes;
short biBitCount;
int biCompression;
int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
int biClrUsed;
int biClrImportant;
};
PictureHeader Picture;
struct RGBcolors {
char R;
char G;
char B;
};
RGBcolors Rgb;
char dane;
string str;
string bottom = "111111111111111111111111111111111";
int main(int argc, char*argv[]) {
FILE *f = fopen("Maze.bmp", "rb");
system("cls");
printf("\n Info about BITMAP\n\n");
fread(&File.bfType, sizeof(File.bfType), 1, f);
printf(" Typ: %x", File.bfType);
fread(&File.bfSize, sizeof(File.bfSize), 1, f);
printf("\n Size of file: %d bytes", File.bfSize);
fread(&File.bfReserved1, sizeof(File.bfReserved1), 1, f);
printf("\n Reserved2: %d", File.bfReserved1);
fread(&File.bfReserved2, sizeof(File.bfReserved2), 1, f);
printf("\n Reserved2: %d", File.bfReserved2);
fread(&File.bfOffBits, sizeof(File.bfOffBits), 1, f);
printf("\n");
fseek(f, 14, SEEK_SET);
fread(&Picture.biSize, sizeof(Picture.biSize), 1, f);
printf("\n Size of header: %d", Picture.biSize);
fread(&Picture.biWidth, sizeof(Picture.biWidth), 1, f);
printf("\n Width: %d px", Picture.biWidth);
fread(&Picture.biHeight, sizeof(Picture.biHeight), 1, f);
printf("\n Heigh: %d px", Picture.biHeight);
fread(&Picture.biPlanes, sizeof(Picture.biPlanes), 1, f);
fread(&Picture.biBitCount, sizeof(Picture.biBitCount), 1, f);
printf("\n Bits per pixel: %d (1, 4, 8, or 24)", Picture.biBitCount);
fread(&Picture.biCompression, sizeof(Picture.biCompression), 1, f);
printf("\n Compression: %d (0=none, 1=RLE-8, 2=RLE-4)", Picture.biCompression);
fread(&Picture.biSizeImage, sizeof(Picture.biSizeImage), 1, f);
printf("\n Size of only a image: %d", Picture.biSizeImage);
fread(&Picture.biXPelsPerMeter, sizeof(Picture.biXPelsPerMeter), 1, f);
fread(&Picture.biYPelsPerMeter, sizeof(Picture.biYPelsPerMeter), 1, f);
fread(&Picture.biClrUsed, sizeof(Picture.biClrUsed), 1, f);
fread(&Picture.biClrImportant, sizeof(Picture.biClrImportant), 1, f);
fseek(f, File.bfOffBits, SEEK_SET);
cout << "\n\n\n Structure:\n";
int bmpImg;
int c = 0;
for (int i = File.bfOffBits; i < File.bfSize; i++)
{
fseek(f, i, SEEK_SET);
fread(&bmpImg, 1, 1, f);
if ((i - File.bfOffBits) % 96 == 0)
{
cout << endl;
if (i != File.bfOffBits)
str += "1\n";
c++;
}
if ((i - File.bfOffBits) % 3 == 0) {
printf("%d", (bmpImg == 255 ? 1 : 0));
str += (bmpImg == 255 ? "1" : "0");
}
}
printf("\n");
fclose(f);
str += "1\n";
str += bottom;
{
}
if (str.length() > 1)
{
FILE *plik = fopen("maze.map", "w");
for (int i = 0; i < str.length(); i++)
{
fprintf(plik, "%c", str[i]);
}
fclose(plik);
}
return 0;
}
位图大小为 32x32。
例如。
dev-c++ 的正确结果
visual studio 结果不正确:
感谢任何建议和指导
已解决
刚刚用我从 fread 读取到的任何值变量初始化。