by Dan East » Jan 29, 2002 @ 3:26pm
When you read via a struct, like you are attempting to do, numerical values must be stored in raw binary format. An int is 32 bits, thus you would have raw binary of 4 bytes / chars to represent that int. There also is no padding or line feeds between the members of the struct. Of course you cannot generate such datafiles with a standard text editor like NotePad. Basically if you want to read data in that manner, you must save it in that manner - fill the myobjects struct with data, then write it out to file. You would then be able to read using the simple method you desire. This method also has the advantage of creating much smaller data files; ints and floats are stored more efficiently in binary format than in human-readable form, and there would be no whitespace or comments. Load times would also be extremely fast because the data can be loaded straight into memory in blocks.
If you want to parse plain-text, human readable strings into binary data (as your sample data indicates), then try fscanf or similar. You can also write your own custom routines to parse the data yourself by examining the data one char at a time. Also, the addition of comments adds extra difficulty to the parsing, because the comments would be optional. You would have to properly handle empty lines, spaces before the values, etc. Basically you determine how flexible you want to code your loading routines, which dictates the guidlines that must be followed in creating / editing the data files.
Dan East