Reading a .3DS file

Hey all,

I'm having some trouble reading a .3DS file. I've done it in other programming languages, but can't get it working in C++ (I just started working with C++ yesterday).

How can I read 4 bytes from the file into an int variable? At the moment I'm trying this but I don't really know what I'm doing:

1
2
3
4
5
6

   ifstream file ("HORNET_L.3DS", ios::in|ios::binary);

   char chunkLength [sizeof(unsigned int)];
   file.read(chunkLength, sizeof(chunkLength));


From what I've seen (and I haven't seen much at all!) the first argument passed to the read function has to point to a char array. Then the data read from the file is placed in that array.

So my question is: If I've done it correctly up to there, how can I convert that character array into a single integer?

Hope to hear from you soon,

Thanks in advance, Nick :)
Last edited on
From what I know of file I/O, that code looks good.

You can convert a character array to an integer using the atoi(const char * str) function
You can convert a character array to an integer using the atoi(const char * str) function

But not in this case, as chunkLength contains machine representation of an integer.

I would do it like this:
1
2
int chunkLength;
file.read(reinterpret_cast<char *>(&chunkLength), sizeof(chunkLength));
Thanks for both the replies :)

Abramus your code worked perfectly! I hope I'm not asking for too much, but do you think you could give a quick explanation of what "reinterpret_cast<char *>" is doing?

Thanks heaps again, Nick!
reinterpret_cast is just a method to perform a "non-standard" conversion, usually used when converting between different kinds of pointers (like in this case). It has a few friends:
static_cast,
const_cast,
dynamic_cast.

You could also use "(char *)&chunkLength", but it is not considered to be a good code in C++ (this conversion method is derived from C).

EDIT: http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
You should be aware that typecasting like that is not safe -- you are telling the compiler to shut up if it wants to complain that you may be making a big mistake.

What if int has a different size than 4 on someone's machine?
What if this gets compiled on a Big-Endian architecture?

When performing binary I/O, it is always best to properly pack and unpack the data yourself. More on binary I/O methods here. http://www.cplusplus.com/forum/beginner/11431/

Hope this helps.
Hey Duoas,

Thanks for your reply, I looked at that thread you pointed me to. Didn't quite understand it - my C/C++ knowledge is still only 2 days old haha Hopefully I'll be able to understand it soon!

I do have another question though. From the file I'm reading I'm trying to read an object name. Now when I open to the file in notepad I can see the names scrambled amongst the "jibberish" in clear strings, however when I read them using the code below I get the letters plus some symbols. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

                while (i <= 20 && strcmp((const char*)nameChar,"") != 0)
                {

                    strcat(objectName,nameChar);
                    file.read(nameChar, sizeof(nameChar));
                    i = i + 1;

                }


                newObject.name = objectName;

                cout << objectName << "\n";


Opening the file in notepad I'll see the object name: @¶ HornSeat A§ (showed surroundings)
However my code will output: H☻o☻r☻n☻S☻e☻a☻t☻ (those are smiley faces)

Haha I really have no Idea what is going on there. Unfortunately for you guys/girls I'm pretty sure you will be seeing a lot of me around here over the next couple months lol

Thanks in advance, Nick


Last edited on
NickPaul,

If I remember correctly, names in 3DS files are strings termineted with '\0'. If so, then you can easily read them using the following code:

1
2
std::string name;
std::getline(file, name, '\0');


Duoas,

You're right. I just didn't want to scare off a fresh C++ programmer by telling him that you need 10 lines of code to read an integer:)
Topic archived. No new replies allowed.