Re: parsing a file..



broli wrote:

So here's my attempt using STATE MACHINE (as many people have
suggested) for reading the .zeus file in its proper format. Please
tell me what are the flaws in this program (Please do not point
obvious things such as why I have not taken care of error handling eg.
What if fp is null ? what if fscanf fails ? Where is the if-else for
that situation ? etc etc. I am just assuming that everything works out
fine and theres no corruption issues with the file. I just wanted to
know if the logic is correct and ofcourse I want to implement error
handling later).


There are 8 states in this program -

STATE 0 - 1st line of the .zeus file i.e.
(TAB)13001(TAB)POLYGON_SURFACE (ignored, move to next state)
STATE 1 - 2nd line of the .zeus file i.e. (TAB)name(TAB)TRIANGLES
(ignore, move to next state)
STATE 2 - 3rd line of the .zeus file i.e. (TAB)nvert[602 in eg. i
gave] (TAB)0(TAB)ntri[1200 in eg. i gave](TAB)0(TAB)101 (take the
nvert and ntri values as input, move to next state)
STATE 3 - new line..appears twice in the file. First time at 4rth line
and then again after entries for vertices end and before entries for
the triangles start. (ignore, move to next state)
STATE 4 - Entries for the ertices begin. Remain in this state till all
the nvert vertices are read. After that move to state 5.
STATE 5 - New line again. Ignore and move.
STATE 6 - Read the entries for all the triangles.
STATE 7 - If the sequence is 0(TAB)END it means the end of file has
been reached. So this state represents the end.



#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct Vector
{

double x, y, z;

}Vector;

typedef struct Vertex
{

Vector V;

} Vertex;

typedef struct Triangle
{

int v0, v1, v2;

} Triangle;

typedef struct Object
{

int nvert, ntri;
Vertex *Vert;
Triangle *Tri;

} Object;


int main(void)
{

FILE *fp;
char buf[100];
int STATE;
Object obj;
char *START = " 13001 POLYGON_SURFACE";
char *END = " 0 END";

You should use the \t escape sequence instead of hardcoded tabs.

char NL[] = { '\n', '\0' };

fp = fopen("sphere.zeus", "r");

STATE = 0;

label:while(!feof(fp))

This is wrong. Feof() and ferror() are suitable only AFTER a I/O
function has returned EOF. This has been explained numerous times here.
In C you first attempt to read, and if that fails by returning EOF,
*then* you find out if that failure was due to end-of-file or other
error by testing feof() and ferror() respectively.

<snip>

I look at the rest of your code when I get time. I'm sure a better
design is possible.

.



Relevant Pages

  • Re: array of structures.
    ... typedef struct verticesstruct ... int v0, v1, v2; ... triangle *tri; ... form of validity checking. ...
    (comp.lang.c)
  • Re: parsing a file..
    ... machine solution anyway:- ... int nvert, ntri; ... Triangle *Tri; ...
    (comp.lang.c)
  • Re: array of structures.
    ... typedef struct verticesstruct ... int v0, v1, v2; ... triangle *tri; ... Anything other than zero is regarded as "true" in C. ...
    (comp.lang.c)
  • Re: parsing a file..
    ... typedef struct Vector ... typedef struct Triangle ... int nvert, ntri; ...
    (comp.lang.c)
  • Re: parsing a file..
    ... typedef struct Vector ... typedef struct Triangle ... int nvert, ntri; ...
    (comp.lang.c)