Thread: writing 2D matrix to binary

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    writing 2D matrix to binary

    This code works perfectly, but due to performance, I want to make the write function one call.
    Code:
            int i=0,k=0;
    	for(i;i<rows;i++)
    		for(k=0;k<cols;k++)
    			fwrite(&matrix[i][k],sizeof(float),1,stdout);
    }
    The following code doesn't work. It causes my columns to be off (image slanting right).
    Code:
    	if(fwrite(matrix,sizeof(float),rows*cols,stdout)<rows*cols)
    	{
    		perror("error");
    		exit(1);
    	}
    Any ideas where I'm going wrong?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    No, if these look different your problem is probably outside of the code you have posted. Since what is printed is not ascii, but will be printed as if it where, there might be tabs and newlines in there messing up the output.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    There must be something going wrong with my read function.

    Code:
    void copyMatrixOrig(float **matrix)
    {
    // i do this to allow for indexing with two values i.e. matrix[0][2]
    	int i=1;
    	for(i;i<rows;i++)
    		matrix[i]=matrix[i-1]+cols;
    
    
    	fread(matrix[0],sizeof(float),rows*cols,stdin);
    
    	return;
    }
    Am I missing something? If I write the matrix I copy out immediately upon returning, using the latter of the given write functions, the image is shifted about 1/5 of the columns to the right, but all the rows are intact.
    If I immediately copy this matrix to a new matrix like this:
    Code:
    	int i=0,k=0;
    	for(i;i<rows;i++)
    		for(k=0;k<cols;k++)
    			temp[i][k]=matrix[i][k];
    And write this matrix to file, the image is distorted like I mentioned in the original post. I'm really thrown for a loop because if I write it using the the first way, everything works.

    By the way, I dealing with PGM files which is how I graphically know everything is off.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Is the matrix a block of contiguous floats that happens to be indexed 2-dimensionally, or is it an array of pointers which are distinct allocated blocks (rows)? In your working case, you are strictly outputting the elements. In the non-working case you may be outputting addresses mixed in with the data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. sort elements of 2D matrix by 1st column reference
    By cfdprogrammer in forum C Programming
    Replies: 12
    Last Post: 05-04-2009, 03:26 PM
  3. Writing to my 2D Array
    By qwertysingh in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 01:16 PM
  4. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM
  5. Help!! Tips on Matrix Calculator Program please!
    By skanxalot in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2002, 11:26 AM