About Binary files.
I need help with binary files that contain user-defined structures. I have just saved a file using Qbasic 4.5 that has a structure that looks like this:
TYPE points
x as integer
y as integer
z as integer
END TYPE
This has been saved in the binary format, and was wondering how to open it in C++ Is there a way to open this file in C++. Because I have made a brilliant level editor for my game using Qbasic 4.5 but wanna open it in a DJGPP.
Thanks in advance.
Dark Star.
---------------------------------------------You Only Live Once - Don't be afriad to take chances.
OK first of all I want to let you know that I don''t know any thing about QBasic or DJGPP so I am writing this from the perspective of a VC++ user. It should still apply however.
The first thing you need to do is find out how QBasic stores a structure. I would assume that QBasic stores an integer in a single WORD (16bits) and that would make the QBasic structure 6 bytes long. I am also assuming that QBasic writes out the data in the structure as it is stored in memory when you write to a file in binary mode.
If this is the case, then you would need to define a structure on the C side that corisponds to the structure on the QBasic side.
struct qbpoints
{
short x;
short y;
short z;
};
Then you could just use some code like...
qbpoint Points;
int hfile = open("path\filename.ext", _O_BINARY / _O_RDWR);
int bytesread = read(hfile, &Points, sizeof(qbpoints);
You might also need to ensure that the compiler is packing the structure on 16bit boundries instead of 32bit boundries as this may cause the structure above to become 8 bytes long instead of the expected 6 bytes.
The first thing you need to do is find out how QBasic stores a structure. I would assume that QBasic stores an integer in a single WORD (16bits) and that would make the QBasic structure 6 bytes long. I am also assuming that QBasic writes out the data in the structure as it is stored in memory when you write to a file in binary mode.
If this is the case, then you would need to define a structure on the C side that corisponds to the structure on the QBasic side.
struct qbpoints
{
short x;
short y;
short z;
};
Then you could just use some code like...
qbpoint Points;
int hfile = open("path\filename.ext", _O_BINARY / _O_RDWR);
int bytesread = read(hfile, &Points, sizeof(qbpoints);
You might also need to ensure that the compiler is packing the structure on 16bit boundries instead of 32bit boundries as this may cause the structure above to become 8 bytes long instead of the expected 6 bytes.
yes there is, here some C code to get you started (probably wont compile since im doing right out of my head though
);
struct points
{
// assuming an integer is 2 bytes in QB, right?
short int x;
short int y;
short int z;
};
points p;
FILE *f = fopen("filename.abc", "rb"); // rb meaning read-binary
fread(&p, sizeof(points), 1, f);
fclose(f);
Check the docs for more info

struct points
{
// assuming an integer is 2 bytes in QB, right?
short int x;
short int y;
short int z;
};
points p;
FILE *f = fopen("filename.abc", "rb"); // rb meaning read-binary
fread(&p, sizeof(points), 1, f);
fclose(f);
Check the docs for more info
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement