Advertisement

Reading content of a text file.

Started by October 31, 2003 05:38 PM
1 comment, last by eduardor2k 21 years, 4 months ago
Hi it's me again. I know i don't stop posting a lot of stupid thing, but for me it's my only way to progress on my work, i hope you will understand. Here is my doubt , I want to load some data from a ext file, i'm using lesson10 from nehe who already loads data, but i would like to load some extra data. For example: ----------structure--------------- TEXTURES: (nº of textures) (It follows the textures to load) etc.. ----------structure--------------- The code from nehe's is this: //************** START *********************// void readstr(FILE *f,char *string) { do { fgets(string, 255, f); } while ((string[0] == '/') || (string[0] == '\n')); return; } void SetupWorld() { float x, y, z, u, v; int numtriangles; FILE *filein; char oneline[255]; filein = fopen("data/world.txt", "rt"); // File To Load World Data From readstr(filein,oneline); sscanf(oneline, "NUMPOLLIES: %d\n", &numtriangles); sector1.triangle = new TRIANGLE[numtriangles]; sector1.numtriangles = numtriangles; for (int loop = 0; loop < numtriangles; loop++) { for (int vert = 0; vert < 3; vert++) { readstr(filein,oneline); sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v); sector1.triangle[loop].vertex[vert].x = x; sector1.triangle[loop].vertex[vert].y = y; sector1.triangle[loop].vertex[vert].z = z; sector1.triangle[loop].vertex[vert].u = u; sector1.triangle[loop].vertex[vert].v = v; } } fclose(filein); return; } //***************** end *****************// I though adding this lines to extract extra data: sscanf(oneline, "TEXTURES: %d\n", &numtextures); adding this compiles fine under vc++7, but when i execute the program i gives me an error abou allocation error or something like this. but for the rest i don't know how to do it. i though using fread from stdio.h and stdlib.h someone could explain me how it works. Thanks Again. Thank you for reading this post, but if you can answer it better. [edited by - eduardor2k on October 31, 2003 6:42:09 PM]
Thank you for reading this post, but if you can answer it better. :)
If you look you can see that every sscanf() has readstr(filein,oneline); before so adding

readstr(filein,oneline);
sscanf(oneline, "TEXTURES: %d\n", &numtextures);

would read in the number of textures. readstr() is what actually reads from the file and sscanf evaluates the data read from the file. If you highlight a word in visual c++ and press f1 you will get information about that command from the helpfile. It is a very useful feature when trying to understand how sourcecode works.
if (!understand) ask;
Advertisement
Thanks, you were right.
I didn''t knew i have to copy:
readstr(filein,oneline);
everytime i woud read a line, but NOW IT WORKS, so i don''t have problems doing it that way.

I found how to load the other info.

Thanks a lot "pjupju".

Thank you for reading this post, but if you can answer it better.
Thank you for reading this post, but if you can answer it better. :)

This topic is closed to new replies.

Advertisement