PDA

View Full Version : Program can't find file's passed to it


Phlex
09-04-2009, 08:16 PM
Hey, I've been using the ray trace tutorial here - http://www.codermind.com/articles/Raytracer-in-C++-Depth-of-field-Fresnel-blobs.html - and when I tried to use the source, I encountered a problem.
The sample source code is set up to read information from a txt file, it seems to work fine, although, when I pass in the names of images for a skybox, it does not work, they are defined like this in the txt file




Cubemap.Up = alpup.tga;
Cubemap.Down = alpdown.tga;
Cubemap.Right = alpright.tga;
Cubemap.Left = alpleft.tga;
Cubemap.Forward = alpforward.tga;
Cubemap.Backward = alpback.tga;



These are then read as such in the application:



myScene.cm.name[cubemap::up] = sceneFile.GetByNameAsString("Cubemap.Up", emptyString);
myScene.cm.name[cubemap::down] = sceneFile.GetByNameAsString("Cubemap.Down", emptyString);
myScene.cm.name[cubemap::right] = sceneFile.GetByNameAsString("Cubemap.Right", emptyString);
myScene.cm.name[cubemap::left] = sceneFile.GetByNameAsString("Cubemap.Left", emptyString);
myScene.cm.name[cubemap::forward] = sceneFile.GetByNameAsString("Cubemap.Forward", emptyString);
myScene.cm.name[cubemap::backward] = sceneFile.GetByNameAsString("Cubemap.Backward", emptyString);




The function works fine, however, when it gets to this part of the code:



currentfile.open(name[up].c_str(), ios_base::binary);
if ((!currentfile)||(!dummyTGAHeader(currentfile, sizeX, sizeY)))
return false;



it returns false, and I don't know why, the files are in the same directory as the executable, but still, currentfile is not opening the file as it returns false. Any ideas? Thanks.

Reedbeta
09-05-2009, 12:26 AM
Make sure that the current directory when you run the project is really set to the same directory as the executable. If this is Visual Studio for instance, it will be executed from the project directory while the executable is usually in a Release or Debug subdirectory. Alternatively, try specifying full paths for the files.

Phlex
09-05-2009, 02:45 AM
Hey, I'm running the program outside of the IDE in the debug folder, so I don't need to worry about that. I've tried adding the entire file path but still no luck. I've checked that the strings are correct using this:


cout << name[up].c_str() << endl;


and they all have the correct string names, so I don't see why ifstream.open() is failing.

Wernaeh
09-05-2009, 07:09 AM
You sure it is really the open() call that fails, and not the dummyTGAHeader() one ?

Cheers,
- Wernaeh

Phlex
09-05-2009, 03:18 PM
Yeah it is, I separated them into separate if statements, if dummyTGAHeader is called first it will obviously fail because it relies on currentfile. So anyway, I ran

if(!currentfile)

and it still failed.

Reedbeta
09-05-2009, 07:41 PM
Are you in *nix, or Windows? If *nix, do the filenames match in case?

If you write a minimal testbed app and try to open the file, does it work?

Phlex
09-06-2009, 02:16 AM
I'm on windows. When I ran a minimal testbed app, it worked fine when I used a

const char*

rather than using

name[up].c_str()

So I tried doing this:

const char* up = name[up].c_str();

currentfile.open(up, ios_base::binary);

But it wasn't happy
Also as a side note, I had to put in full directory paths, and double slashes to avoid escape sequence errors.

Phlex
09-06-2009, 02:37 AM
Ok so I've fixed it, there were several problems that were coming up. The main one was that the process of reading in the strings representing the file paths skipped tabs, new lines and spaces, so without spaces, folders like Documents and Settings, were copied incorrectly.