Archived

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

llvllatrix

Path loading - 10 hours debugging and counting

Recommended Posts

llvllatrix    340
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?

Share this post


Link to post
Share on other sites
Oluseyi    2103
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!

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


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

Share this post


Link to post
Share on other sites