Skip to main content
GameDev.net gamedev.net
🔒 Locked

failbit set after seekg (0, std::ios_base::beg)?

Started by Crypter Dec 13, 2007 at 9:31 PM 1 replies 3.6k views
Original Post
Crypter
Crypter
Hey everyone, I have an odd issue with my code that I have never accounted before, so am hoping for any suggestions (Where should I even look?) I am currently integrating my new TER codec into my engine. In this process, it is now using my engines file manager. (Which uses std::fstream) The problem is that the stream (std::fstream) is failing to retrieve data from the file! Perhaps code can explain it better then I can...

std::fstream* stream = rkFile.GetStream();  // This is okay and works well.


// Everything is good. We want to begin reading, so move to the beginning of the
// file (Its a binary file):
stream->seekg (0, std::ios_base::beg);

This is problem number 1. Right after fstream::seekg(), the failbit is set. What can cause the failbit to get set here? If we exclude this line, and just read from it... (We should be at the beginning of the file, anyway):

	rkHeader.m_magic[0] = stream->peek();

...Then the eof bit is set instead. (??) I can provide more information if needed, I would like to here, except I dont know where else to search for this one. :/ I cannot read anything but garbage with these error bits getting set for unkown reasons, so cannot proceed further until I get this resolved. I can post more code if needed (Please let me know what areas you would like to see. I cannot post the entire code here, as it is way to big.) Here is the full routine at where the error is:

Kernel::File& operator >> ( _in Kernel::File& rkFile, _out TER_Header& rkHeader) {

	Kernel::g_EvehLog << Kernel::LogLevel (Kernel::INFO) << "...Reading TER Header information..." << std::endl;
	std::fstream* stream = rkFile.GetStream();

	// Everything is good here...
	stream->seekg (0, std::ios_base::beg);

	// After the above, the fail bit is now set :/

	return rkFile;	
}

I am certain the stream is good (as the good bit IS set) Thanks for any help of any kind. Kind and best wishes for this holiday season as well![cool]
Crypter
Crypter
*Ugh... I think I know what the problem is.

When allocating a new fstream object, the file manager seems to be giving it the directory name of the file, not the actual filename. So, the file is never being opened.

But wait...Why was the goodbit being set? hehe....[embarrass]

The good bit was being set because I cleared all bits after allocating the fstream object in hopes of fixing the problem (Which did not work, anyways). I learned my lesson here: Dont try to hide errors behind temporary solutions.

Here is the code, in case anyone is interested, with the error shown:
void File::AllocStream (_in const std::string& strPath) {	// allocate stream buffer //	if (strPath.length()) {// Here, the debugger tells me that the directory string is being passed// instead of the filename. Oops!		m_pkStream = new std::fstream (strPath.c_str(), std::ios::in | std::ios_base::binary);// Failbit is set now. In hopes of fixing this problem, I cleared all bits// thinking the above should never fail anyways.....  bad, I know.		m_pkStream->clear();	}}


Thanks for anyone who read this silly mishap [smile]

*goes off to fix that problem now...*
Crypter
Crypter
Hey everyone,

Im posting here to let everyone know that its working now[smile]

Thanks for all that have taken their time to read this thread.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.