fwrite not writing property (or fread not reading properly)

This is a discussion on fwrite not writing property (or fread not reading properly) within the C++ Programming forums, part of the General Programming Boards category; I am trying to write to a dat file that contains a header on how many entries there are as ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    fwrite not writing property (or fread not reading properly)

    I am trying to write to a dat file that contains a header on how many entries there are as well as the entries themselves. Below is the file writing code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <malloc.h>
    
    int main()
    {
    	float slavepos[] = {.083, .21, .072, 0, 0, 0};
    	float delta = -0.001;
    	int numslavepos = 100;
    
    	float *slaveposwrite = (float*) malloc(100*6*sizeof(float));
    
    	char *FileName = "NeedleBasePos.dat";
    	FILE *fid;
    
    	fid = fopen(FileName, "w");
    
    	fwrite(&numslavepos, sizeof(int), 1, fid);
    
    	for (int i = 0; i < 100; i++)
    	{
    		memcpy(&slaveposwrite[6*i], &slavepos[0], 6*sizeof(float));
    		slavepos[1] += delta;
    	}
    
    	fwrite(slaveposwrite, sizeof(float), 6*100, fid);
    	
    	fclose(fid);
    	free((void*) slaveposwrite);
    	return 0;
    }
    As I read from the debugger, I'm getting something like the following that I'm assuming gets written to the file attached as arraywritten.txt

    However, when I try to read the file in another code, while I'm reading the header properly in terms of the amount of entries, I am having trouble reading the entries properly. Below is my code for reading the file, and I have attached the garbage that I have read in arrayread.txt:

    Code:
    void SPHandleError(int i)
    {
    	char errormsg[200];
    	switch(i) {
    	case 1: sprintf(errormsg, "Error opening file NeedleBasePos.dat !"); break;
    	case 2: sprintf(errormsg, "Input file has an incompatible data structure\n"); break;
    	default: sprintf(errormsg, "An unhandled error has occured !!");
    	}
    	//if (MessageBox(NULL, errormsg, "Unrecoverable Error - Closing application", MB_ICONERROR))  exit(-1);
    }
    
    // Reading the needle trajectory from a text file
    // STATUS UPDATED: SlavePos
    void SlavePosFromFile( char *fname, slavepos_struct *needletippos )
    {
    #define SPHEADERSIZE		1
    
    	FILE	*spfile;
    	int		Sizes[SPHEADERSIZE], *Sizesptr = Sizes, pos;
    
    	if( (spfile = fopen(fname, "rb")) == NULL)
    	{
    		SPHandleError(1);
    	}
    	else
    	{
    		printf("Bytes read for Needle Position trajectory from file '%s':\n", fname);
    	}
    	printf("NeedleBasePos.dat size = %d\n", fread(Sizes, sizeof(int), SPHEADERSIZE, spfile));
    		needletippos->entries = *(Sizesptr++);
    
    	needletippos->SlavePos = (float*) calloc(SLAVEPOSELEMENTS*needletippos->entries, sizeof(float));
    	printf("SlavePos = %d\n",
    		   fread(needletippos->SlavePos, sizeof(float), SLAVEPOSELEMENTS*needletippos->entries, spfile));
    
    	// Check to see if more data is left in the file
    	pos = ftell(spfile);
    	fseek(spfile, 0, SEEK_END);
    	if (ftell(spfile) != pos) SPHandleError(2);
    	fclose(spfile);
    	printf("Parameter file '%s' is read successfully.\n", fname);
    }
    I don't know what possibly could cause me not being able to read (or write) the values properly. Any suggestions would be much appreciated.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Nvm my question. Opened and read the file inconsistently in terms of the fopen flags.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	free((void*) slaveposwrite);
    If you are actually using C rather than C++, then perhaps you should change the name of your source file to .c instead of .cpp, that way you do can convert any pointer to or from void * without a cast.

    Although this is the C++ forum, so perhaps you wanted to use new [] and delete [] to create your local array. In which case you probably should use a ofstream/ifstream to write/read the data through, and using the write() and read() member functions of the streams.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I'm using C++ object-oriented functionalities in some parts of the code. However, I haven't changed it that this structure is also object-oriented. That's why I left the casting in there along with using free() function. Also, I had been using this file as a mex file to be run in the Matlab environment. Therefore, I kept using free in order to remain compatible with allocating memory in the right memory space in Matlab. Thanks for your inputs though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 03:45 PM
  2. Replies: 2
    Last Post: 12-26-2008, 04:38 PM
  3. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 03:07 PM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 07:57 PM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21