Problem whit sfcanf and huge file
Hi people! i have a problem whit a huge file in txt format , i try to read this TXT very long file and i use fscanf , but i only can read 900 elements of this file , anyone have a trick or something to read a big txt file? here is the code i use
/*************************************/
FILE * fp;
float *vertex = new float;
int *faces = new int;
fp=fopen(name, "r");
setbuf(fp, buf );
fscanf(fp,"%d",&num_vtx);
printf("num vtx = %d\n",num_vtx);
printf("start loading vtxs\n");
for(int i=0;i<(num_vtx*3);i++){//24
printf("%d ",i);
fscanf(fp,"%f",&vertex);
}
fscanf(fp,"%d",&num_faces);
printf("\nnum faces:%d\n",num_faces);
printf("start loading faces\n");
for(int j=0;j<(num_faces*3);j++){//12
printf("%d ",j);
fscanf(fp,"%d",&faces[j]);
}
typedef struct {float x,y,z;} tVertex;tVertex *_Vertexes = NULL;BOOL readVtx ( const char *filename ){ FILE *fhd = NULL; if ( ( fhd = fopen (filename, "rt" ) ) == NULL ) return FALSE; fscanf ( fhd, "%d",&_numVtx ); printf ( "Number of vertexes : %d\n", _numVtx ); _Vertexes = malloc ( sizeof (tVertex)*_numVtx ); for ( int i = 0; i < (_numVtx); i++ ) fscanf ( fhd, "%f %f %f", &_Vertex[i].x, &_Vertexes[i].y, &_Vertexes[i].z ); fclose ( fhd ); return ( TRUE );}
Should work, add some validation. And the vertex file should be in format:
x y z
x y z
...
ie:
2
1.0 1.0 1.0
2.0 2.0 2.0
And remember to free allocated memory. This was written on top of my head, but I think you get the picture.
First, why are you using
setbuf
? And if you do, you should be using setvbuf
, because setbuf
in antiquated.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement