CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2001
    Location
    US,NY
    Posts
    25

    How to read blocks of float[15] from a binary file???

    I thought the following would read in blocks of 15 floats from a binary file but after a bunch of correct reads all the output numbers are giberish, numbers like 1E-20 type stuff..

    I have to use stdio routines like fread, I have written the same code with fstreams and it works fine. I am missing some stdio quirk or something.. It seems to get hosed when the _cnt variable in the FILE structure becomes < then the size of 15 floats (60).. I believe it has to do with the buffering of the file, which is the system 4096 size.

    I have done more debugging.. _cnt gets to a value of 1 then the next fread reads data that is 1516 bytes downstream instead of 60 (note that this is not even a multiple of 60, my block size).. for no reason, I have no seeks or anything in the code..

    float CPDATA[15];
    do{
    fread((char*)CPDATA,sizeof(float)*15, 1, thO->fd);
    //outfile read info to a file
    outfile<<ftell(thO->fd)<<" ";
    for(int i=0;i<15;i++)
    outfile<<CPDATA[i]<<" ";
    outfile<<endl;

    k++;
    }while(k<j);
    Last edited by delic; November 2nd, 2002 at 04:44 PM.

  2. #2
    Join Date
    Oct 2002
    Posts
    36
    Ok, my C sucks as I have been using C++ for ages (much nicer IMO ), so any nasties here, please forgive me.

    You shoulnd have to look at the members of the FILE struct for something as simple as this, my guess is a problem with your code

    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    #define MY_MAX_FLOATS 15
    
    int main(){
    
    	FILE *out = 0,*in = 0;//File pointers
    	float* ptrFloat = 0;//memory I will use to read file
    	char *szFileName = "Hello.txt";//Name of file
    	int i;//iterator
    	float flArray[MY_MAX_FLOATS];//Array I will write to file
    	
    	for(i = 0;i < MY_MAX_FLOATS;i++)
    		flArray[i] = i+1 * 1.1;//fill array wilth float info
    	
    	out = fopen(szFileName,"wb");//open output file
    	if(!out){
    		puts("Error opening file");
    		return 1;
    	}
    	
    	fwrite(flArray,sizeof(float)*MY_MAX_FLOATS,1,out);//Write info to file
    	if(ferror(out)){
    		puts("Error writting to file");
    		fclose(out);
    		return 1;
    	}
    	
    	fclose(out);//close output file
    	
    	//*********************************************************************
    	// We now have a file with 15 floats that we want to read
    	//*********************************************************************
    	
    	in = fopen(szFileName,"rb");//open file to read
    	if(!in){
    		puts("Error opening file");
    		return 1;
    	}
    	
    	ptrFloat = malloc(MY_MAX_FLOATS * sizeof(float));//allocate memory for 15 floats
    	if(!ptrFloat){
    		puts("Error allocating memory");
    		fclose(in);
    		return 1;
    	}
    	
    	fread(ptrFloat,sizeof(float)*MY_MAX_FLOATS,1,in);//read all that data
    	if(ferror(in)){
    		puts("Error reading file");
    		fclose(in);
    		return 1;
    	}
    	
    	for(i = 0;i < MY_MAX_FLOATS; ++i){
    		printf("%.2f\n",ptrFloat[i]);//write to screen
    	}
    	
    	fclose(in);//free file
    	free(flArray);//And memory
    	return 0;
    }
    Last edited by dumah; November 3rd, 2002 at 03:52 AM.

  3. #3
    Join Date
    May 2001
    Location
    US,NY
    Posts
    25
    Well thanks for the reply.. Yeah it has to do with the porting of the code to windows from unix..

    Aggh, I agree c++ is much nicer and that is why this was one of those cases where it is easier to rewrite the code using fstreams. And I did and it worked great..

    It had to have been some wierd stdio bug or something because I literally rewrote each line of code with an equivalent line of fstream code and it works great.. So in the end I still dont know the C code doesnt work but I went around the probelm..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured