Hey guys,
I'm parsing this WaveFront .obj file, still (for those that have read the previous post), but it has some really weird error in it that I haven't seen in a program before.
The parsing algorithm starts out like this:
Open the file in textmode.
Get filesize
Allocate buffer with calloc, size: filesize + 1
Read file in buffer
Count the lines
Now, the filesize is correct, and reading goes okay too: ferror() doesn't report any errors.
The linecount is, however, off! :( I'm using this code:
printf("Counting lines and determining maximum length...");
int maxLen, lenCount;
int lines;
for (i = lines = lenCount = maxLen = 0; i <= fileSize; i++){
if (data[i] == '\n'){
lines++;
lenCount++;
if(lenCount > maxLen) maxLen = lenCount;
lenCount = 0;
}
lenCount++;
}
printf("Counted %d lines, maximum length: %d\n", lines, maxLen);
In short:
current char a newline?
Yes:
add one to lines and chars
update max string length
No:
add one to chars
Well, I couldn't think that that piece of code is wrong since it's so simple. Besides, it worked with numerous files and I've used that same code in the past. But the linecount is off. So I output char *data. Just to make sure, the output (cut away a lot, it's a 458KB file ;))
f 2483//3522 2134//3498 2028//3437
f 2527//3523 2028//3437 2906//3436
f 2555//3524 2527//3523 2906//3436
f 2556//3525 2555//3524 2906//3436
f 2558//3526 2556//3525 2906//3436
f 2558//3526 2906//3436 2560//3515
f 2527//3523 2483//3522 2028//3437
//3525 2906//3436
f 2558//3526 2906//3436 2560//3515
f 2527//3523 2483//3522 2028//3437
41 2860//3237 2863//3240
f 2800//3183 2813//3184 2861//3238
f 2865//3242 2861//3238 2813//3184
f 2861//3238 2865//3242 2862//3239
f 2866//3243 2862//3239 2865//3242
f 2813//3184 2818//3191 2865//3242
f 2867//3244 2865//3242 2818//3191
f 2865//3242 2867//3244 2866//3243
f 2868//3245 2866//3243 2867//3244
While it SHOULD have been:
f 2483//3522 2134//3498 2028//3437
f 2527//3523 2028//3437 2906//3436
f 2555//3524 2527//3523 2906//3436
f 2556//3525 2555//3524 2906//3436
f 2558//3526 2556//3525 2906//3436
f 2558//3526 2906//3436 2560//3515
f 2527//3523 2483//3522 2028//3437
For some reason I get more data then there is! The lines it reads in are correct, but it keeps reading! I know the buffer has plenty of data to store it all, I've changed the size (filesize+1) to filesize+200, +800, hell, even +2000, just to make sure. It's calloc'ed, so it's cleared...
I don't know what to try anymore. Any help is greatly appreciated, it's driving me mad.
Thanks in advance,
Nick