D
Darkness
Guest
Once again, im interested in writing a program to convert a file format. Heres the question. How do I read from hex/binary in C?
#include <stdio.h> //library with file utilsvoid main( void ) //program starts here{ FILE *f; //file handle long l; // vv i define few variables here vv float f; char ch; char name[20]; f = fopen( "file.dat", "rb" ); //rb = open file for reading in binary mode if ( f == NULL ) //if could not open => exit return; fread( &l, 4, 1, f ); //lets read one long from file. it is 4 bytes long fread( name, 1, 20, f ); //now string 20 bytes long fread( &f, sizeof( float ), 1, f ); //and float which is 4 bytes long fread( &ch, 1, 1, f ); //and char is 1 byte long fclose( f ); //close file}
void main() //program starts here { FILE *fr;//file handle FILE *fw; unsigned long l, interval_1, interval_2, end, numverts, startpoint; // vv i define few variables here vv char ch; char name[80]; char vrtx[4]; float x, y, z; startpoint = 0; fr = fopen( "File.mdx", "rb" ); //rb = open file for reading in binary mode fw = fopen( "File2.obj", "w" ); //w = write? if ( fr == NULL ) //if could not open => exit return; fseek( fr, 0L, SEEK_END); end = ftell (fr); do { for (; startpoint < end; startpoint++) { fseek( fr, startpoint, SEEK_SET); fread( &vrtx, 1, 4, fr ); if((vrtx[0] == 'V') && (vrtx[1] == 'R') && (vrtx[2] == 'T') && (vrtx[3] == 'X')) { startpoint++; break; if(startpoint > end) { goto a; } } } fread( &numverts, 4, 1, fr ); for (int counter = 0; counter < numverts; counter++) { fread( &x, 4, 1, fr ); fread( &y, 4, 1, fr ); fread( &z, 4, 1, fr ); fprintf( fw, "v %f %f %f\n", x, y, z); } } while (ftell(fr) < end); a: fclose( fw ); fclose( fr ); //close file
; fr = fopen( "File.mdx", "rb" ); // >!< we should move check directly after trying-to-open if ( fr == NULL ) //if could not open => exit return; // >!< "w" should work too, but "wt" means write in text mode fw = fopen( "File2.obj", "wt" ); //w = write? fseek( fr, 0L, SEEK_END); end = ftell (fr); do { // >!< for loops are usually done like this for ( startpoint = 0; startpoint < end; startpoint++) { fseek( fr, startpoint, SEEK_SET); fread( &vrtx, 1, 4, fr ); // >!< easier compare if ( memcmp( vrtx, "VRTX", 4 ) == 0 ) { break; / >!< not sure, but i think that this won't execute, because of break if(startpoint > end) { goto a; } } } fread( &numverts, 4, 1, fr ); for (int counter = 0; counter < numverts; counter++) { fread( &x, 4, 1, fr ); fread( &y, 4, 1, fr ); fread( &z, 4, 1, fr ); fprintf( fw, "v %f %f %f\n", x, y, z); } } while (ftell(fr) < end); a: fclose( fw ); fclose( fr ); //close file
for ( startpoint = 0; startpoint < end; startpoint++) {
fseek( fr, startpoint, SEEK_SET);
FFTactic_Boy: could you please send me an example files (input and output) that do that ? they should not .... And also an program version number please ... i'll try to fix it asap.
#include <stdio.h> //library with file utils #include <iostream.h>#include <string.h>void main() //program starts here { FILE *fr;//file handle FILE *fw; unsigned long end, numverts, startpoint, currentpos, numnorm, numtvert, numtri; unsigned short a, b, c; char vrtx[4], norm[4], tvert[4], face[4]; char inname[20]; char outname[20]; cout << "Enter MDX Filename: "; cin.get(inname, 20); cin.ignore(80,'\n'); cout << "Enter OBJ Filename: "; cin.get(outname, 20); cin.ignore(80,'\n'); float x, y, z; startpoint = 0; currentpos = 0; fr = fopen( inname, "rb" ); //rb = open file for reading in binary mode if ( fr == NULL ) //if could not open => exit return; fw = fopen( outname, "wt" ); //w = write text mode fseek( fr, 0L, SEEK_END); end = ftell (fr); while (startpoint < end) { for (; startpoint < end; startpoint++) { fseek( fr, startpoint, SEEK_SET); fread( &vrtx, 1, 4, fr ); if(memcmp( vrtx, "VRTX", 4 ) == 0) { cout << "Geoset Found!\n"; break; } if(ftell( fr) >= end) { return; } } fread( &numverts, 4, 1, fr ); cout << "Vertices: " << numverts << endl; for (int counter = 0; counter < numverts; counter++) { fread( &x, 4, 1, fr ); fread( &y, 4, 1, fr ); fread( &z, 4, 1, fr ); fprintf( fw, "v %f %f %f\n", x, y, z); } fseek( fr, 4L, SEEK_CUR); fread( &numnorm, 4, 1, fr ); cout << "Normals: " << numnorm << endl; for (counter = 0; counter < numnorm; counter++) { fread( &x, 4, 1, fr ); fread( &y, 4, 1, fr ); fread( &z, 4, 1, fr ); fprintf( fw, "vn %f %f %f\n", x, y, z); } //faces for (startpoint = ftell(fr); startpoint < end; startpoint++) { fseek( fr, startpoint, SEEK_SET); fread( &face, 1, 4, fr ); if(memcmp( face, "PVTX", 4 ) == 0) { fread( &numtri, 4, 1, fr ); cout << "Faces: " << numtri << endl; cout << ftell(fr) << endl; for (counter = 0; counter < (numtri / 3); counter++) { fread( &a, 2, 1, fr ); a++; fread( &b, 2, 1, fr ); b++; fread( &c, 2, 1, fr ); c++; fprintf( fw, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a, a, a, b, b, b, c, c, c); } break; } } //end faces //tvert for (startpoint = ftell(fr); startpoint < end; startpoint++) { fseek( fr, startpoint, SEEK_SET); fread( &tvert, 1, 4, fr ); if(memcmp( tvert, "UVBS", 4 ) == 0) { fread( &numtvert, 4, 1, fr ); cout << "TVerts: " << numtvert << endl; cout << ftell(fr) << endl; for (counter = 0; counter < numtvert; counter++) { fread( &x, 4, 1, fr ); fread( &y, 4, 1, fr ); fprintf( fw, "vt %f %f\n", x, y); } break; } } //end tvert startpoint++; } fclose( fw ); fclose( fr ); //close file }