Best/simplest way to load data from disk

Moderator
Posts: 365
Joined: 2002.04
Post: #1
I feel really stupid asking this because it's probably the first thing you get taught if you have a formal education in programming, but since I'm self taught and I've always hated learning about I/O... Blush

I want to store my data for maps, animations and so on as text files so that I can edit them by hand. My first thought is to use either C++ streams or C stdio in text mode. I can read my books and look up tutorials to discover how to actually do it, but I wondered if anyone can give pros and cons for choosing one over the other.

I've also been using the Core Foundation XML parser for data files, but I find it rather cumbersome. Is there anything else that would be a better solution for loading really simple text data?

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Expat is a good XML parser if that's what you want.

C's stdio is probably the easiest way to read files.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #3
Thanks, I'll look into Expat (possibly with a C++ wrapper). I'm also doing some experiment with C's stdio right now... hopefully I'll have figured it out in a few days.

If anyone else has any other thoughts or opinions, I'm still keen to hear them!

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #4
Quote this message in a reply
Jesse
Unregistered
 
Post: #5
I went through the same thing, and found Navigation Services to be a little overwhelming. I ended up using fopen(), etc., which seems to work fine.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #6
Quote:Originally posted by codemattic
also check out tiny xml

That looks very good, especially in terms of being pretty simple and written in C++! I may well end up using tinyxml.

Quote:Originally posted by Jesse
I went through the same thing, and found Navigation Services to be a little overwhelming. I ended up using fopen(), etc., which seems to work fine.

Well, Navigation Services is supposed to help you locate the file using a dialog - it doesn't actually open the file for you. Are you referring to FSOpenFork(), FSRead() etc.?

Incidentally, how are you storing your data in your files? Do you have a sort of formal syntax or is it just a sequence of unlabelled data?

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Jesse
Unregistered
 
Post: #7
Quote:Well, Navigation Services is supposed to help you locate the file using a dialog - it doesn't actually open the file for you. Are you referring to FSOpenFork(), FSRead() etc.?
Yeah, I know. But stdio still seems easier.

Quote:Incidentally, how are you storing your data in your files? Do you have a sort of formal syntax or is it just a sequence of unlabelled data?
Most of what I've done so far has been loading other file formats - Quake levels, 3DS models, etc. What I hope to do, though, is implement stream constructors for a lot of my classes, in which case the data will be in a block mirroring the member data of the class. (As always, I could be wrong about whether this is a good way to go about it...)
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Careful about byte-swapping...
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #9
Quote:Originally posted by Jesse
...the data will be in a block mirroring the member data of the class.

Be careful about member data layout as well. You probably shouldn't be trying to load a block of data directly over an instantiated object in C++ unless you know exactly what you're doing.

I'm going to keep experimenting with cstdio, but I reckon I'm going to carry on using XML because its built in support for hierarchic structure is better than anything I can be bothered to write! My plan when loading from XML is like this:

When I encounter an element which represents an C++ object, I create (new) one of those objects. I then configure the newly created object by calling methods on it for each of the attributes it supports. I use attributes for singular parameters and child elements when I either want to support multiple occurrences of the same thing or if I want something processed in sequence. For example, I might use child elements of a 'shape' element to apply a series of transforms or something.

The important idea I'm trying to apply is that the objects I'm creating don't need to know anything about what kind of data file they're being loaded from. Instead, they're created and configured by a separate loader class.

Does that sound sensible?

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Jesse
Unregistered
 
Post: #10
Just to clarify, I'm not blindly superimposing the data block from the file on the member data block of the class - I'm just reading the values and assigning them.

Also, yes, I had to deal with byte swapping when writing the 3DS importer.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  unable to load app with loader sefiroths 2 2,940 Nov 29, 2011 01:12 PM
Last Post: sefiroths
  Sound from disk in Carbon? Fenris 6 4,079 Feb 12, 2003 10:43 PM
Last Post: Fenris