Advertisement

I/O .. output, input, binary, free.. How to?

Started by December 28, 2000 11:00 PM
0 comments, last by HellRiZZer 24 years ago
Ok, say i got a file like an .ini Hwo do i read this with any mode? I can''t say input string$ all the time.. anyway, i need tutorial on using this kind of structure as in .ini [Tiles Layer 1] 1,1,1 2,1,1 3,1,1 ... .... [End Tiles Layer 1] [Objects Layer] [Trees] 4,5,1 4,9,1 ... [End Trees] [End Objects Layer] Any help?
I''m not sure I understand your question, but I''ll pretend I do.

The biggest consideration with you example is whether the sections have to be in a fixed sequence. If they do then it is very easy. First you have an input routine that skips blank lines and comments if they are allowed. Generally it should compress out whitespace as well unless the whitespace is significant such as indicating heirarchical relationships. If whitespace is used to seperate fields then of course you don''t want to strip all whitespace, but rather all but one character of whitespace. If statements can be split across lines then it should contatenate them together. With a syntax such as C/C++ that isn''t a good way to do it, but with a syntax that requires a continuation character at the end of the line it is simple enough to do that way. You just check if the last non-whitespace character is the continuation character. If comments can follow it then you have to process the line from the front to the continuation character instead.

Now if everything has to be in a fixed order all you have to do is check the line you got. Is the first one the one that is suppose to be first? With your example is it the text "[Tiles Layer 1]". You might want to force it to upper or lower case to make it easier editing. If it is the text you are looking for then you loop until you read the text "[End Tiles Layer 1]". Within the loop you break the line into the individual fields and store it wherever it goes. If the file is complex then you don''t want a function a thousand lines long reading it so you would put each section in it''s own function. If the [Tiles Layer 1] is the same format as [Tiles Layer 2] then you would want to use the same function, i.e. a generic Tiles function. The calling function has to pull out the name and pass the Tiles function a pointer to where it stores the data. It exits on any "[End Tiles" and it is up to the calling routine to make sure the end name is the same. With the subsections within sections it is just a function calling a function that calls a function to read the lowest level of data.

You can have the lowest level functions just return a structure of the type it is suppose to read with the calling function being responsible for storing it permentantly. This is particularly useful with complex data types where it may be far easier to tell what it is after it is parsed and you may need to go through several stages of qualification to accurately classify it.

The only real challenging thing is that the lowest level routines will read the line that the calling routines need. You can''t tell you hit the end until you actually read it. So the calling routines need a means to access the last line read. Overall you really do just say input string$ all the time. Your input routine reads one line from the file into the same variable all the time. It may then bypass that line and immediately read another or concatentate several physical lines to create a logical line. Either way it always returns the same variable with a logical line in it. Those logical lines are then broken up into individual fields. Those fields may be stored where they go permentantly or returned to the function that called that function in the same variables all the time in which case it is the calling functions responsiblity to store it permentantly.

It is really no differant that calling a function that calculates the distance between two points. Rather than being a calculation it is I/O. At the lowest level you are saying give me the next physical line in the file. A step up from that you are saying give me the next logical line. A step up from that give me the next record. A step up from that give me a list of records. A step up from that give me these sections. A step up from that give me this file.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement