Counting lines
I''m writing a RAW file loader, and in my implementation (sp) I have // for comments of any type, and than just the vertex data. What I need to know is how many lines there are so I can count the # of triangles (anyone got a better idea on figuring out how far to loop?) I might be thinking this all wrong... thanks.
||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
August 30, 2000 04:13 PM
1) count first, then re-read the data
2) store number of triangles in the file and read this first
3) read data and stop at the end of the file counting as you go
2) store number of triangles in the file and read this first
3) read data and stop at the end of the file counting as you go
Well how would I even count?
Anyways I figured it out on accident hehe. But now my program is crashing. Heres my code. Obvisuly right now it loads only 1 triangle...:
#include "stdio.h"
#include "string.h"
#include "iostream.h"
#define COMMENT "//"
#define TEST ""
typedef struct Vertex_TYP
{
float x;
float y;
float z;
}Vertex;
typedef struct Triangle_TYP
{
Vertex x;
Vertex y;
Vertex z;
}Triangle;
class Mesh
{
public:
Triangle *triangles;
int trinumber;
};
char buffer[2000];
Mesh *box = new Mesh; //create new Mesh object with a pointer.
void ReadNextString(char buffer[], FILE *file)
{
////////////////////////////////////////////////////////////////////////
// Read the next string that isn''t a comment
////////////////////////////////////////////////////////////////////////
bool SkipLine = 0; // Skip the current line ?
int ScanReturn = 0; // Return value of fscanf
// Skip all strings that contain comments
do
{
// Read new string
ScanReturn = fscanf(file, "%s", buffer);
// Is rest of the line a comment ?
if (!strncmp(buffer, COMMENT, sizeof(COMMENT)))
{
// Skip the rest of the line
fgets(buffer, sizeof(buffer), file);
SkipLine = 1;
}
else SkipLine = 0;
} while (SkipLine == 1);
};
int Count(FILE *file)
{
int count = 0;
while (!feof(file))
{
ReadNextString(buffer, file);
//count
if (!strncmp(buffer, COMMENT, sizeof(COMMENT)))
{
count++;
}
}
return(count);
};
void LoadRaw(char *filename, Mesh *&mesh)
{
FILE *file = fopen(filename, "r");
char buffer[256]; //buffer for reading info
*&mesh = new Mesh;
int count = Count(file);
int lines = count/9;
rewind(file); // start at the beginning
while (!feof(file))//keep going till end of file is reached
{
ReadNextString(buffer, file);
if (strncmp(buffer, COMMENT, sizeof(COMMENT)))
{
// 1 triangle
fscanf(file, "%f %f %f %f %f %f %f %f %f",
mesh->triangles->x.x,
mesh->triangles->x.y,
mesh->triangles->x.z,
mesh->triangles->y.x,
mesh->triangles->y.y,
mesh->triangles->y.z,
mesh->triangles->z.x,
mesh->triangles->z.y,
mesh->triangles->z.z);
// Next vertex
}
}
};
FILE *file = fopen("box.raw", "r");
Mesh *mesh = new Mesh;
void main()
{
LoadRaw("box.raw", mesh);
cout << mesh->triangles->x.x <<
mesh->triangles->x.y <<
mesh->triangles->x.z <<
mesh->triangles->y.x <<
mesh->triangles->y.y <<
mesh->triangles->y.z <<
mesh->triangles->z.x <<
mesh->triangles->z.y <<
mesh->triangles->z.z;
};
It seems to be crashing at
mesh->triangles->z.z);
// Next vertex
} <--- right here.. thats what the debugger says.
Please help
||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
Anyways I figured it out on accident hehe. But now my program is crashing. Heres my code. Obvisuly right now it loads only 1 triangle...:
#include "stdio.h"
#include "string.h"
#include "iostream.h"
#define COMMENT "//"
#define TEST ""
typedef struct Vertex_TYP
{
float x;
float y;
float z;
}Vertex;
typedef struct Triangle_TYP
{
Vertex x;
Vertex y;
Vertex z;
}Triangle;
class Mesh
{
public:
Triangle *triangles;
int trinumber;
};
char buffer[2000];
Mesh *box = new Mesh; //create new Mesh object with a pointer.
void ReadNextString(char buffer[], FILE *file)
{
////////////////////////////////////////////////////////////////////////
// Read the next string that isn''t a comment
////////////////////////////////////////////////////////////////////////
bool SkipLine = 0; // Skip the current line ?
int ScanReturn = 0; // Return value of fscanf
// Skip all strings that contain comments
do
{
// Read new string
ScanReturn = fscanf(file, "%s", buffer);
// Is rest of the line a comment ?
if (!strncmp(buffer, COMMENT, sizeof(COMMENT)))
{
// Skip the rest of the line
fgets(buffer, sizeof(buffer), file);
SkipLine = 1;
}
else SkipLine = 0;
} while (SkipLine == 1);
};
int Count(FILE *file)
{
int count = 0;
while (!feof(file))
{
ReadNextString(buffer, file);
//count
if (!strncmp(buffer, COMMENT, sizeof(COMMENT)))
{
count++;
}
}
return(count);
};
void LoadRaw(char *filename, Mesh *&mesh)
{
FILE *file = fopen(filename, "r");
char buffer[256]; //buffer for reading info
*&mesh = new Mesh;
int count = Count(file);
int lines = count/9;
rewind(file); // start at the beginning
while (!feof(file))//keep going till end of file is reached
{
ReadNextString(buffer, file);
if (strncmp(buffer, COMMENT, sizeof(COMMENT)))
{
// 1 triangle
fscanf(file, "%f %f %f %f %f %f %f %f %f",
mesh->triangles->x.x,
mesh->triangles->x.y,
mesh->triangles->x.z,
mesh->triangles->y.x,
mesh->triangles->y.y,
mesh->triangles->y.z,
mesh->triangles->z.x,
mesh->triangles->z.y,
mesh->triangles->z.z);
// Next vertex
}
}
};
FILE *file = fopen("box.raw", "r");
Mesh *mesh = new Mesh;
void main()
{
LoadRaw("box.raw", mesh);
cout << mesh->triangles->x.x <<
mesh->triangles->x.y <<
mesh->triangles->x.z <<
mesh->triangles->y.x <<
mesh->triangles->y.y <<
mesh->triangles->y.z <<
mesh->triangles->z.x <<
mesh->triangles->z.y <<
mesh->triangles->z.z;
};
It seems to be crashing at
mesh->triangles->z.z);
// Next vertex
} <--- right here.. thats what the debugger says.
Please help
||--------------------------||
Black Hole Productions
www.bhpro.com
simon@bhpro.com
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement