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:
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?
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?
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).
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?
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((constchar*)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