If that is your whole Coordinates file, then you're missing 1 line (line 15 with the first line being numbered 0) that the 4th line of CoordinateGroups wants to use.
I moved this to C because the code you posted wasn't full/compilable. I did my best to cobble together the OpenGL init pieces because they weren't included. I don't think the way I did it would cause issues. Here is the code:
I took out any dynamic memory allocation. If you have to maintain this code in the future, that seems for the best, and it wasn't necessary anyway. I added some error checking (this would have helped point out a lot of your problems, such as reading off the end of a file). A big fix is how getCoords returns the floats read that we'd been insisting you deal with. While the return values were correct right after you called the function, you never printed after subsequent calls, or calls to other functions. If you had, you'd have seen that your values were changing out from under your nose (I did this with the code you posted and saw this result). Now a pointer to one dimension of a statically-allocated 2D array of floats is passed in, and the values of the three floats read are stored immediately into this array. Another big fix is how line and lineptr are used so you don't overflow line, which seemed to be a big source of trouble. CoordinateGroups' first 3 lines are 47 characters, so once you read line 4 you were off the end of line and ruining other parts of the stack.
I didn't take care of:
Checking the error for atoi/strtof as chown33 suggested
Using the "magic" constant of 50 instead of a #define
Passing fp2 to getCoords (i just made fp and fp2 global because they weren't declared in the code you posted and i was just trying to get the code to compile)
I don't remember what else I changed and a diff would be hard to decipher because of the Objective-C to C changes, etc. but I'm sure you can make the comparisons. I think what you need to copy where should be apparent. All of the returns might need to be pulled out of main when you paste it back to makeDisplayList, but you should have some way to indicate an error has occurred.
I hope very much that this is a personal project for fun or to learn, and not a project for school or work. If it is for school hopefully a prof catches you cheating, if it's for work I hope you admit that you're in over your head if you're tasked with something like this in the future (also, providing an address where a consulting invoice could be mailed would be helpful if this is for work). I don't want to make accusations, but you refused to disclose what this is for after many requests.
In the future, providing all of your code and all of your data to begin with will save everyone a lot of second-guessing. Also, it might help to be more agreeable when people ask you if you've tried things or request details about a project or your skill level. Many times you insisted you understood pointers, but if so this is superficial and doesn't include an understanding of memory layout, memory management, etc. which is a critical part. This was at the core of many of the issues, and stating that your understanding was not particularly solid would have helped us guide you (likely guide you immediately away from dynamic memory management). When suggestions were made you were defensive or rude when everyone is trying to help you. No one is responding just to give you a hard time (not normally, at least). You might not get 100% correct information (I made an error in an example, and because i was looking at the thread on my phone missed that you had switched to rewind), but people are doing their best to assist you.
In any event, I hope this is helpful and that you've learned some things from myself and others providing advice in this thread.
-Lee
EDIT: It should be clear that i have no idea if this is error or bug free. I'm sure others can find problems with it. My goal was to make it better, but I'm sure it's not perfect (even beyond the shortcomings i detailed above).
I moved this to C because the code you posted wasn't full/compilable. I did my best to cobble together the OpenGL init pieces because they weren't included. I don't think the way I did it would cause issues. Here is the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GLUT/glut.h>
FILE *fp,*fp2;
void getCoords(int i, float *a);
int main(int argc, char *argv[]) {
char *lineNumber,*geometricType,*lineptr;
char line[50];
int num;
fp = fopen("CoordinateGroups.txt", "r");
if(!fp) {
fprintf(stderr,"Could not open CoordinateGroups.txt");
return -1;
}
fp2 = fopen("floats.txt", "r");
if(!fp2) {
fprintf(stderr,"Could not open floats.txt");
return -2;
}
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );
glutCreateWindow("Test");
glNewList(1, GL_COMPILE);
while (fgets(line,50,fp) != NULL) {
lineptr = line;
geometricType = strsep(&lineptr, ";");
if(!geometricType) {
fprintf(stderr,"Could not read the geometric type from line: %s\n",line);
return -3;
}
if(strlen(geometricType) == 0) {
fprintf(stderr,"Could not read the geometric type from line: %s\n",line);
return -4;
}
num = atoi(geometricType);
//This is for GL_QUADS. I have not yet added GL_TRIANGLES coordinate parsing code.
if (num == 4) {
float coords[4][3];
int i;
for (i = 0; i < 4; i++) {
lineNumber = strsep(&lineptr, ",");
if(!lineNumber) {
fprintf(stderr,"Invalid line specification read from line: %s\n",line);
return -5;
}
if(strlen(lineNumber) > 0) {
int len = strlen(lineNumber);
if(lineNumber[len-1] == ';') lineNumber[len-1] = '\0'; //Could probably be unconditional if you're positive about the format
} else {
fprintf(stderr,"Invalid line specification read from line: %s\n",line);
return -6;
}
getCoords(atoi(lineNumber),coords[i]);
printf("%f, %f, %f\n", coords[i][0], coords[i][1], coords[i][2]);
}
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
{
glVertex3f(coords[0][0], coords[0][1], coords[0][2]);
glVertex3f(coords[1][0], coords[1][1], coords[1][2]);
glVertex3f(coords[2][0], coords[2][1], coords[2][2]);
glVertex3f(coords[3][0], coords[3][1], coords[3][2]);
}
glEnd();
}
}
glEndList();
fclose(fp);
fclose(fp2);
return 0;
}
void getCoords(int i, float *a) {
rewind(fp2);
char line[50];
char *strptr = line;
int j;
for (j = 0; j < i + 1; j++) {
strptr = fgets(strptr, 50, fp2);
if(!strptr) {
fprintf(stderr,"Could not read line %d from float.txt, trying to read to line %d.\n",j,i);
return;
}
}
for(j = 0; j < 3; j++) {
char *val = strsep(&strptr, ";");
if(!val) {
fprintf(stderr,"Invalid token in coordinates file, line: %s\n",line);
return;
}
a[j] = strtof(val,NULL);
}
}
I took out any dynamic memory allocation. If you have to maintain this code in the future, that seems for the best, and it wasn't necessary anyway. I added some error checking (this would have helped point out a lot of your problems, such as reading off the end of a file). A big fix is how getCoords returns the floats read that we'd been insisting you deal with. While the return values were correct right after you called the function, you never printed after subsequent calls, or calls to other functions. If you had, you'd have seen that your values were changing out from under your nose (I did this with the code you posted and saw this result). Now a pointer to one dimension of a statically-allocated 2D array of floats is passed in, and the values of the three floats read are stored immediately into this array. Another big fix is how line and lineptr are used so you don't overflow line, which seemed to be a big source of trouble. CoordinateGroups' first 3 lines are 47 characters, so once you read line 4 you were off the end of line and ruining other parts of the stack.
I didn't take care of:
Checking the error for atoi/strtof as chown33 suggested
Using the "magic" constant of 50 instead of a #define
Passing fp2 to getCoords (i just made fp and fp2 global because they weren't declared in the code you posted and i was just trying to get the code to compile)
I don't remember what else I changed and a diff would be hard to decipher because of the Objective-C to C changes, etc. but I'm sure you can make the comparisons. I think what you need to copy where should be apparent. All of the returns might need to be pulled out of main when you paste it back to makeDisplayList, but you should have some way to indicate an error has occurred.
I hope very much that this is a personal project for fun or to learn, and not a project for school or work. If it is for school hopefully a prof catches you cheating, if it's for work I hope you admit that you're in over your head if you're tasked with something like this in the future (also, providing an address where a consulting invoice could be mailed would be helpful if this is for work). I don't want to make accusations, but you refused to disclose what this is for after many requests.
In the future, providing all of your code and all of your data to begin with will save everyone a lot of second-guessing. Also, it might help to be more agreeable when people ask you if you've tried things or request details about a project or your skill level. Many times you insisted you understood pointers, but if so this is superficial and doesn't include an understanding of memory layout, memory management, etc. which is a critical part. This was at the core of many of the issues, and stating that your understanding was not particularly solid would have helped us guide you (likely guide you immediately away from dynamic memory management). When suggestions were made you were defensive or rude when everyone is trying to help you. No one is responding just to give you a hard time (not normally, at least). You might not get 100% correct information (I made an error in an example, and because i was looking at the thread on my phone missed that you had switched to rewind), but people are doing their best to assist you.
In any event, I hope this is helpful and that you've learned some things from myself and others providing advice in this thread.
-Lee
EDIT: It should be clear that i have no idea if this is error or bug free. I'm sure others can find problems with it. My goal was to make it better, but I'm sure it's not perfect (even beyond the shortcomings i detailed above).