Advertisement

Array Anxiety

Started by August 18, 2000 03:56 AM
6 comments, last by RogueFiend 24 years, 4 months ago
i am trying to write a cob file loader and so far i have managed (with help from nate's ase loader tutorial) to get my program to be able to get basic info from the cob file such as the number of vertices. i can succesfully write a program that reads the file and obtains the correct info but when i go to assign an array of floats, to hold the correct amount of vertices, it won't compile. i have pasted the code bellow. can someone please help me.
            
#include <stdio.h>

#include <string.h>


char fileData[256];
FILE *stream;
int numVertex;
int numTexVertex;
int numFaces;

int main()
{
	if(!(stream = fopen("box.cob", "r")))
	{
		printf("Could not open file. Please make sure it exists.\n\n");

		return 0;
	}

	rewind(stream);

	while(!feof(stream))
	{
		fscanf (stream, "%s", &data);

		if(!strcmp(data, "World"))
		{
			fscanf(stream, "%s", &data);
			if(!strcmp(data, "Vertices"))
			{
					fscanf(stream, "%d", &numVertex);
			}
		}

		if(!strcmp(data, "Texture"))
		{
			fscanf(stream, "%s", &data);
			if(!strcmp(data, "Vertices"))
			{
					fscanf(stream, "%d", &numTexVertex);
			}
		}

		if(!strcmp(data, "Faces"))
		{
			fscanf(stream, "%d", &numFaces);
		}

	}

	float verts[numVertex][3];
	float texVerts[numTexVertex][2];

	printf("Number of vertices = %d\nNumber of TexVertices = %d\nNumber of Faces = %d\n\n", 
			numVertex, numTexVertex, numFaces);
	
	return 0;
}
        
Edited by - RogueFiend on 8/18/00 4:02:43 AM Edited by - RogueFiend on 8/18/00 4:03:21 AM
How many times do i have to punch the fucking monkey?
You can''t size an array with a variable in a declaration, unfortunately. You''ll have to use new.
Here''s what you wrote:
    float verts[numVertex][3];float texVerts[numTexVertex][2];[/source]Here''s how it would work:[source]typedef float tVertex[3];typedef float tTexVertex[2];tVertex *verts = new tVertex[numVertex];	tTexVertex *texVerts = new tTexVertex[numTexVertex];    


( Might be a slight error in the typedefs, but you get the idea )


Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
the compiler most know at COMPILE time how big the array is going to be. You cant use a variable to create an array...
where you have
float verts[numVertex][3];
float texVerts[numTexVertex][2];
cant do that
but you can create it dynamically like this
    struct VERTICE{   float x, y, z;};VERTICE* verts = new VERTICE[numVertex];VERTICE* texVerts = new VERTICE[numTexVertx];    

now just use them like you normally would
then before you exit the program do this
delete [] verts;
delete [] texVerts;



"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

*lol* Looks like we both replied the same thing at the same time.


Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
I am not quite sure of how to implement what you said about the typedefs. How would I store vertices in the array elements? and how would I retrieve them. I tried just going:

            for(index = 0; index < numVertex; index++){	fscanf("%f %f %f", &verts[index][0],                           &verts[index][1],	                   &verts[index][2]);}            


but the compiler complains:

fscanf' : cannot convert parameter 1 from 'char [9]' to 'struct _iobuf *'.

Can you please clear things up for me. BTW thanks for answering my post.






Edited by - RogueFiend on August 19, 2000 8:53:31 AM
How many times do i have to punch the fucking monkey?
Did you include the FILE* in that fscanf? I forget what param it is (1 or 4) but it must be in there somewhere.

Check out my favorite C standard library reference (also includes source for the functions).

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Edited by - ImmaGNUman on August 19, 2000 11:15:05 AM
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Advertisement
Man do I feel like a dumbass. Maybe late night coding isn''t for me.
How many times do i have to punch the fucking monkey?
MadKeithV can you please modify my code so that it uses the typedefs for I am still unsure of how to do what you said.
How many times do i have to punch the fucking monkey?

This topic is closed to new replies.

Advertisement