Path loading - 10 hours debugging and counting

Started by
6 comments, last by llvllatrix 21 years, 11 months ago
Heres the problem: Im trying to load a text file, which tells me where to find other files I need to load. The text file loads successfully and gives me the paths i want. When i tried to load the files with these paths, the ifstream constructor fails. Anyone know why?
Advertisement
First time through (if you used the same ifstream object), you probably used something along the lines of while( !fin.eof() ) where fin is an ifstream object. When that loop terminates (ie, EOF), the failbit is set. Before performing any additional operations on the ifstream object, call clear to clear the failbit: fin.clear();

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
Doesn''t work. Whenever i load a new object i create a new ifstream, so i dont think that is it. This function works when i pass it a hard coded string like "data/stuff.ms3d" but not when i pass it a variable string. Here''s what it looks like:

bool MilkshapeModel::loadModelData( const char *filename )
{
ifstream inputFile( filename, ios::in | ios::binary | ios::nocreate );
MessageBox( NULL, filename, "Error", MB_OK | MB_ICONERROR );
if ( inputFile.fail())
//Fails right here
return false; // "Couldn''t open the model file."

Could it be the formatting on the string i pass it, ie null character. I tried this to get rid of it:

char * string;
int i = strlen(modelFilename);

string = (char *)malloc(sizeof(char) * i);

if (string == NULL)
{
MessageBox( NULL, "Insufficient memory", "Error", MB_OK | MB_ICONERROR );
pModel = NULL;
delete(pModel);
return(false);
}

_mbsnbcpy( (unsigned char *)string, (unsigned char *)modelFilename, sizeof(char) * i);

but still nothing.
What''s inside filename when you''re NOT hardcoding it?

If you''re loading let''s say "data/stuff.ms3d" and it references "otherstuff.ms3d", then otherstuff is supposedly located in data/ folder, but you''re opening it as if it was in current folder. You need to either 1) prepend relative path to filename ("data/" in this case) or 2) change current process directory to data/.
---visit #directxdev on afternet <- not just for directx, despite the name
What is your file format, and how do you read the file? Depending on the answer, you may have a trailing newline character on the filename string. That would cause a file open to fail, but the filename would look ok if you printed it out to the screen. Basically, you''d be passing "/dir/file.ext\n" to the open function, which isn''t a valid filename. This is a common problem when using things like fgets(), but I don''t know how ifsteam deals with newlines.
quote:Original post by IndirectX
What''s inside filename when you''re NOT hardcoding it?

If you''re loading let''s say "data/stuff.ms3d" and it references "otherstuff.ms3d", then otherstuff is supposedly located in data/ folder, but you''re opening it as if it was in current folder. You need to either 1) prepend relative path to filename ("data/" in this case) or 2) change current process directory to data/.


Indirect is right about that. Your program will attempt to open all files starting in your program''s path. (If you''re running a program in c:\mygame\test1\) and you tell it to open ''filea.txt'' the system implies that you want to open "c:\mygame\test1\filea.txt".

-ATR-
Just something totally unrelated:

Your code contained:
<code>
pModel = NULL;
delete(pModel);
</code>

You are going to get a memory leak from that , delete pModel first then assign it a null value :D
Check the string you''re getting from the file. Very likely, it has a trailing ''\n'', which would make your filename string look like "data\n/stuff", which won''t work.

This topic is closed to new replies.

Advertisement