I am writing an importer for a custom export script to load mesh information for models.

I tested my algo with small files, 20-30 kb of information with success, but when I try something
bigger like 400kb(not that big mind you), the file fails to open.

This is the significant code related to my issue.

void DrawEngine::LoadMeshFromLW(std::vector<CUSTOMVERTEX*> &cVertex, std::string lwFile)
{
	#pragma region Declare/Initialize globals and verify stream integrity

		std::fstream *importLW = new std::fstream;
		std::string labelTitle;
		int labelRelated = 0; //Hold information relevant to the set of data. Vertice count, face count, etc... 
		//TODO (labelRelated) as far as I can tell, will always be int, but might have to make string if label related information isnt an int value later in the future.
		int fileSize = 0;		
		int faceCount = 0;

		//Open file, get file size, then make sure stream pointer is put back at the beginning of the stream. 
		//The entire time, checking the stream to make sure everything is ok.
		importLW->open(lwFile, std::ios::in);//TODO Look into std::ios::nocreate instead
		if(importLW->is_open() && importLW->good())
		{
			importLW->seekg(0, std::ios::end);
			fileSize = (int)importLW->tellg();
			importLW->seekg(0, std::ios::beg);

			if(importLW->tellg() != (std::ios::pos_type)0)
			{
				//Error- Stream position not at the beginning of file.
				MessageBox(0, "Error", "Error", 0);
			}
		}
		else
		{
			//Error- File open failure
			MessageBox(0, "Error", "Error", 0);
		}

	#pragma endregion

From line 15, it jumps to line 30.

Pasting ruined format alittle bit, sorry.

Any ideas? Thanks.

Hi, im not sure what compiler your using but it is possible that it uses 16 bit ints by default? This means that with your 30kb file the size would be under the 65k limit of 16 bit int, and then the 400k files overflows the int?

Just thought it would be something worth checking. Although this isnt the bug your seeing it may be possible to come up.

I don't have any insights into your problem, but I'm curious about something. Doesn't the compiler complain on line 14 that lwFile isn't a const char * ? Normally the fstream objects take a C-string (so you'd need lwFile.c_str() in that spot instead).

>> From line 15, it jumps to line 30.

Significantly, all the tellg and seekg's happen BETWEEN lines 15 and 30. The file isn't opening correctly in the first place. Can't see how a file size would have any effect at all here. Perhaps write a small independent program that simply opens and closes the files to check them. Maybe you have a permissions problem or something else?

And make sure that it is indeed skipping from line 15 to 30 and that the error popup isn't actually line 24.

Agreed with the above two posts, one other thing i just thoguht of, is the file you are opening a text file or a binary file?

... the file fails to open.

perror() is likely to be informative in this case.

I don't have any insights into your problem, but I'm curious about something. Doesn't the compiler complain on line 14 that lwFile isn't a const char * ? Normally the fstream objects take a C-string (so you'd need lwFile.c_str() in that spot instead).

That is a good point, I just double checked the parameter and you are correct, but no it is not complaining.

Significantly, all the tellg and seekg's happen BETWEEN lines 15 and 30. The file isn't opening correctly in the first place. Can't see how a file size would have any effect at all here. Perhaps write a small independent program that simply opens and closes the files to check them. Maybe you have a permissions problem or something else?

And make sure that it is indeed skipping from line 15 to 30 and that the error popup isn't actually line 24. .

Right, but you are off the point. I open the file, then check to make sure it is open. It isnt so the conditional fails and my error message pops up. And yes it is moving to line 30, I have debugged and tried to investigate what the problem is and cant figure it out =(.

Agreed with the above two posts, one other thing i just thoguht of, is the file you are opening a text file or a binary file?

It is a text file with a .lw extension. I tried opening it with the binary flag as well just to see what happens, and nothing changes.

What makes this hard to understand what is going wrong, is that like i said earlier, smaller files open just fine and pass the conditionals. This larger file does not =(.

Thanks for investigating

perror() is likely to be informative in this case.

Ill look into this.


Edit: got a "Bad file descriptor" error. Ill look into this right now, feel free to comment though and give me more suggestions!

Edit: Ok guys, Im really sorry for wasting your time, I was passing a string that didnt match the file name. Just a DOH! moment I guess. Thanks for looking into it though!

Dont worry, you wouldent be the first to do something like that, once i seen your "Bad file descriptor" edit i was going to suggest it but looks like you found it.

Can your mark this as solved if it is :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.