edit: I figured it out while writing this: "you want a scripting language with weak typing."
No offense intended friend. I'm just answering honestly and accurately instead of throwing my opinions into the thread. The "best" can only be quantified objectively if there is some hard metric to back it up. The only relevant hard metrics in this case are:
- 1. Waste of the machine.
2. Waste of your typing.
I see you didn't really mean "best" but hear me out...
That's doesn't sound very productive nor optimal thing to do when you can directly properly do it in C# and learn more about that language, which is the point of this thread, I am not trying to have super performance.
The best method in this case would be calling the operating system directly and using block IO. That is the important part, that makes it objectively the best: Using the OS's best method without a heavy series of wrappers around it.
(If you want to get really nitty gritty, you should memory map the file and then just calculate your pointers to your structures and you are done, no loading necessary, a good OS will even use available info to implement lazy loading for you.)
Do you realize that learning a new language in order to write a binary file parser would take me longer than actually just trying to code it with what I currently know (C#)? It would be pretty stupid and counter productive to try to learn a new language like C++ when I am not even half way with C#.
Do you realize that you don't know what you don't know? (And if you are thinking right now that you know what I am hinting at that you don't know, you missed it completely) You are too naive to understand the entire classes of benefits of knowing the machine you are working on and the language that system designers are using. By simply knowing C++ you will have much easier insight into binary structure since they are almost always designed by coders (not scripters who tend towards text formats). So all you _really_ wanted was a "good" method, not the "best", so don't get offended at me for answering accurately.
I might switch to C++ the day I start to see the limitations of C# and need extra performance, for now C# does the job pretty damn well, I am not trying to code a game engine nor a super stable software for a space rocket here.
I don't see any reason you would _have_ to switch, even for those tasks, math is math. A better algorithm always beats a better compiler trick.
Anyways, I ended up making a class for the file structure and made a function that takes a bytes array and reads/fills the fields by type.
Yes maybe C++ already has its own built in function to read a binary file into a structure, but that's not good enough, even if C# might have its own built in function to to do that as well its not what I needed.
I needed to write my own parser so that I could properly parse certain specific data types like vectors into a custom class or yet again parse the strings that have a specific length and which need some cleaning.
Please have an open mind: This is where you show that you don't know what you don't know.
- 1. I did not use the C++ function to read in binary data, that's just as poor as the C# interface. The benefit of using C++ is that you are also using C, you can directly call the OS instead (or through the FILE* wrappers) which is the only candidate for being considered the "best" given that any other approach is going to eventually boil down to this.
2. If you read my example, you should notice all my nested classes (h2_reflexive, h2_dependency) are loaded all in that same one shot. The actual "stupid and counter productive" thing here is writing tons of shitty code that you will throw away instead of re-using because you don't know what you are doing and don't want to learn how to learn because I guess your way of learning is the best despite you needing to go to obscure forums to ask for help.
(hehe yeah, I just said read() is actually exactly what you needed but you are just too nub to even see that.)
No need to comment on the code, plenty of examples in this thread, yours is just fine man, "if it works it works"
Don't get me wrong, I know C++ is way better in many ways in terms of performance optimization.
But clearly programming is not my profession here, I am a 3D artist and C# suits my needs very well, C# is much more accessible and easier to learn, C# also comes with a lot of default libraries which provide a lot of functionality and save time, where as learning and using C++ would take me too long and I don't need that level of complexity to achieve the same results.
It only takes too long if you have that attitude, so stop putting your own life down with negativity. If you learned C# you basically already know C++, provided you are decent with arrays.
...and well... programming IS my profession
and I'm only taking the time to explain because I think it can help.
As far as libraries, you are simply talking about the .NET framework, which is absolutely available on C++.
BUT! in addition to providing all your .NET functions, AND easier interface to the data you are working with... the VAST majority of real-world libraries ONLY work on C++ and often the best you can do in C# is using a so-so wrapper with poor documentation. That's not accessible and SURE AS FUCK not easier. But I guess like you said you will have to see the limitations yourself, although it's sounding like you don't even want to program, you want a scripting language with weak typing.
The only way I am going to see the limitations of C# is learning more about it and experimenting more.
I will also gain the experience from (almost, I guess you can't learn it all) fully learning this language (before moving to another) that I'll be able to use and compare to whatever I learn next and then be able to see what were its advantages and disadvantages.
If you know the grammar and you can portray your points and understand others, you have "fully" learned the language. You are practicing now to get better at actually using the language. Don't be scared man!
tl;dr = I'm not trying to get you off C#, I'm trying to show you the "best" method you asked for, not just another "easy" method since you said you want to LEARN.
(PS I would think you would use google if you really just want an easy method to copy paste into your C# app)