Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Stephen R

ifstream/ofstream headache

This topic is 5244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!