Archived

This topic is now archived and is closed to further replies.

Lode

File reading, parser

Recommended Posts

I always found reading files somewhat difficult and confusing, but this time I encountered the most difficult problem ever: I have a txt file with contents like this: v500 v1 v3 p5 ;lala v601 p3 v50 v3.1415 p50 v1 ;wateenlekkerebanaan But with more lines like that. As you can see, there is always a char with a numerical value that can be a float (or double), and one line consists out of up to 8 of these char-number combinations. At the end of a line CAN be a comment that starts with ";" and this comment should be ignored. Now what I want to write is something that opens this txt file, reads through it line by line. The up to 8 char-number combos from line 1 should go in command[0].c1 to command[0]v8, if there are less than 8 combos on the first line the missing ones get replaced by ''v'', 0. Then the ones from the second line go to command[1].c1 to command[1]v8, and so on until the last line was reached. By c1 I mean char 1 (you know, the first char on that line) and v8 = the 8th numerical value on that line (if there is a 8th one) How could I do this???? I already have difficulties with recognising a line... :/ This is for a sort of emulator test and I want it to read commands from a txt file, now I have to enter commands in the code all the time, reading them from a file is cooler. Thanks.

Share this post


Link to post
Share on other sites
recognize ends of lines by searching for the newline character: ''\n''

when you hit a '';'' character advance to the character after the next ''\n'' character. that character will be your next line

if you could give some more specific details about why you want the file to look like that, i can deactivate my homework alarms and give you more help.

=me

Share this post


Link to post
Share on other sites
It''s not a homework I swear!!! And if you think a person who would make something like this without being forced to is weird, well then I am weird

I thought this form would be the simplest for the emulator I wrote (it''s an emulator for a non-existing alien computer device found on planet Xhah ), and also this form gives the least typing work

Ok if you really want more details, the char means how the number that''s attached to it should be interpreted, and the number itself is a value.

The first char-number combo on a line is the number of the command that should be performed, v101 means "perform command number 101" which is "quit". p101 would mean "perform command number var[50]" where var[50] is a value in the memory (p comes from pointer) so if var[50] is accidently 101 the program will again quit.

**http 500 internal server errors***

The second combo is the condition, v1 means the command is always performed, p1 means the command is performed if var[1] is not 0.

The last 6 combos are variables that will be used or changed by the command, for example,

v603 v1 v50 v50 v255 v255 v25

means run command 603 (= draw pixel) and it will draw a white pixel at position 50,50.

BTW because this way of coding is extremely difficult I plan to extend the parser later so that commands have a real name, like add, pset, ..., but FIRST I want to get it to work this way!

Thank you.

Share this post


Link to post
Share on other sites
I always used strtok() to parse lines retrieved from a file using fgets(). If you detect a syntax error, just throw an error message and skip to the next line. This probably isn''t the most elegant solution, but hell, I program in C. Screw elegance.

"If people are good only because they fear punishment and hope for reward, then we are a sorry lot indeed." - Albert Einstein

Share this post


Link to post
Share on other sites