ifstream/ofstream headache

Started by
1 comment, last by Stephen R 20 years, 3 months ago
Hi, I''m having a bit of trouble with ofstream and ifstream. I create an ifstream in one function, read the contents of a file and close the ifstream. The contents are read successfully and everything is as it should be. If I then go to open a file using ofstream and opening it fails. If I use th exact same function BEFORE I use the function conatining the ifstream it works perfectly. It also effects fopen. fopen works before creating the ifstream but not after. I have also tried taking the code and rearanging it in different ways, puting it all in th esame function, everything. Heres the source to the offending functions

int MzMIFFile::WriteToFile(char* FileName){
	using std::ofstream;
	using std::ios;

	if(!PixelData || !FileName){
		return MZERR_FAILED;
	}

	ofstream File;
	File.open(FileName, ios::out | ios::binary | ios::trunc);
	if(!File){
		return MZERR_INVALIDPARAM;
	}
	
	File.write((char*)&Header,sizeof(MZ_MIF_HEADER));
	File.write((char*)PixelData,(Header.PixelFormat / 8) * Header.Width * Header.Height);

	File.close();
	return MZERR_SUCCESS;
}


int MzMIFFile::LoadFromFile(char* FileName){
	using std::ifstream;

	ifstream File(FileName);
	if(!File){
		return MZERR_INVALIDPARAM;
	}
	
	Destroy();

	File.read((char*)&Header,sizeof(MZ_MIF_HEADER));

	switch (Header.PixelFormat){
		case 16:
			PixelData = new unsigned short[Header.Height * Header.Width];
			break;
		case 32:
			PixelData = new unsigned long[Header.Height * Header.Width];
			break;
		default:
			File.close();
			return MZERR_FAILED;
			break;
	}

	File.read((char*)&PixelData,(Header.PixelFormat/8) * Header.Height * Header.Width);
	File.close();

	return MZERR_SUCCESS;
}
Any help would be greatly appreciated. Thanks -stro
Advertisement
just a quick comment that may/may not work, but try doing File.clear() or whatever the variable is paired with the clear member function. That clears any error states.
Well, R2D22U2..
I tried adding File.clear() to the functions, but it is still not working.
Thanks anyway.

This topic is closed to new replies.

Advertisement