I'm looping through a series of tiff images in a directory. I've determined that the image isn't closing like it's supposed to when I use TiffClose(image);

Code:
#include <stdlib.h>
#include <stdio.h>
#include <tiffio.h>
#include <dirent.h>

int main(int argc, char** argv)
{
  TIFF *image;
  FILE *output;
  DIR *dir;
  struct dirent *entry;
  uint16 photo, bps, spp, fillorder;
  uint32 height, width, rps;
  tsize_t stripSize;
  unsigned long imageOffset, result;
  int stripMax, stripCount;
  char *buffer, tempbyte;
  unsigned long bufferSize, count;

	dir = opendir(argv[1]);
	if (!dir)
	{
		fprintf(stderr, "There was a problem opening the directory\n");
		exit(42);
	}
	
	int m = 0;

	while ((entry = readdir(dir)) && (m<10))
	{
		fprintf(stdout, "\nFile: %d\n", entry->d_name);
		// Open the TIFF image
		if((image = TIFFOpen(entry->d_name, "r")) == NULL)
			fprintf(stderr, "Could not open incoming image\n");
		else
		{
			// Check that it is of a type that we support
			result = TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps);
			if(result == 0)
			{
				fprintf(stderr, "Either undefined or unsupported number of bits per sample\n");
				fprintf(stdout, "Number of bits: %d\n", TIFFTAG_BITSPERSAMPLE);
				//exit(42);
			}
			
			if((TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp) == 0) || (spp != 1))
			{
				fprintf(stderr, "Either undefined or unsupported number of samples per pixel\n");
				//exit(42);
			}
			
			if(TIFFGetField(image, TIFFTAG_ROWSPERSTRIP, &rps) == 0)
			{
			fprintf(stderr, "Either undefined or unsupported number of rows per strip\n");
			//exit(42);
			}
			
			// Read in the possibly multiple strips
			stripSize = TIFFStripSize (image);
			stripMax = TIFFNumberOfStrips (image);
			imageOffset = 0;
			
			bufferSize = stripMax * stripSize;
			if((buffer = (char *) malloc(bufferSize)) == NULL)
			{
				fprintf(stderr, "Could not allocate enough memory for the uncompressed image\n");
				exit(42);
			}
			
			for (stripCount = 0; stripCount < stripMax; stripCount++)
			{
				if((result = TIFFReadEncodedStrip (image, stripCount, buffer + imageOffset, stripSize)) == -1)
				{
					fprintf(stderr, "Read error on input strip number %d\n", stripCount);
					exit(42);
				}
			imageOffset += result;
			}
			
			// Deal with photometric interpretations
			if(TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photo) == 0)
			{
				fprintf(stderr, "Image has an undefined photometric interpretation\n");
				exit(42);
			}
			
			/*
			if(photo != PHOTOMETRIC_MINISWHITE)
			{
				// Flip bits
				printf("Fixing the photometric interpretation\n");
				for(count = 0; count < bufferSize; count++)
					buffer[count] = ~buffer[count];
			}
			*/
			
			// Do whatever it is we do with the buffer -- we dump it in hex
			if(TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width) == 0)
			{
				fprintf(stderr, "Image does not define its width\n");
				exit(42);
			}
			else
			
			if(TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height) == 0)
			{
				fprintf(stderr, "Image does not define its height\n");
				exit(42);
			}
			
			int matrixA[width][height];
			
			for(count = 1; count < width*height; count++)
			{
				int i = (count) % (width);
				int j = (count) / (width);
				matrixA[i][j] = buffer[count];
			}
			
			output = fopen("output.txt", "w");
			if(!output) 
			{
				printf("Oh ........!\n");
				exit(0);
			}
			
			for(count = 0; count < width*height; count++)
			{
				int i = (count) % (width);
				int j = (count) / (width);
				fprintf(output, "%d", matrixA[i][j]);
				if(i == width-1)
					fprintf(output, "\n");
				else 
					fprintf(output, " ");
			}
			
			fprintf(stdout, "Image %d is complete!!\n", m);
			TIFFClose(image);
			fprintf(stdout, "Get the closed image: %d\n", image);
		}
		m++;
	}
}
Here's the output:

File: 5251163
.: Cannot read TIFF header.
Could not open incoming image

File: 5251187
..: Cannot read TIFF header.
Could not open incoming image

File: 5251211
Image 2 is complete!!
Get the closed image: 5283920

File: 5251259
TIFFOpen: img_000000001__000.tif: Cannot open.
Could not open incoming image

File: 5251307
TIFFOpen: img_000000002__000.tif: Cannot open.
Could not open incoming image

File: 5251355
TIFFOpen: img_000000003__000.tif: Cannot open.
Could not open incoming image

File: 5251403
TIFFOpen: img_000000004__000.tif: Cannot open.
Could not open incoming image

File: 5251451
TIFFOpen: img_000000005__000.tif: Cannot open.
Could not open incoming image

File: 5251499
TIFFOpen: img_000000006__000.tif: Cannot open.
Could not open incoming image

File: 5251547
TIFFOpen: img_000000007__000.tif: Cannot open.
Could not open incoming image


The first two files are expected to return an error (they are the ./ and ../ files). The third file opens like it's supposed to and completes. The last line in the third section should cause an error, since that is happening AFTER I've closed the file. I imagine, then, that the file isn't closing at all. I figure the fact that the image isn't closing properly is the reason why the other images are failing to open. Any ideas on how to fix?