views:

119

answers:

4

alt text

Why does the c++ program produce the error shown? I'm especially confused since outfile opens without error yet infile displays the error? Both are defined in xcode exactly the same!! I've altering the "path type" setting without success. The open on infile always fails! Any suggestions would be very much appreciated!!

For those who responded thanks but as you can see but infile and outfile exist and are in the same location:

alt text

+1  A: 

Probably there is no file named "in_file" in the program's working directory, so it can't be opened for reading.

For outfile this doesn't matter since it is opened for writing and if it doesn't exist yet it will just be created.

The directory listing you posted doesn't show where the compiled executable is, but probably it will be somewhere in the build directory. Probably this is then also its working directory and the place where the input file would need to be. (Look for the out_file the program creates when it is run, it will be created in the working directory, the same directory where it searches for in_file. And it is not the directory you posted the listing of, the out_file there is too old.)

sth
please send my update in the question
ennuikiller
but shouldn't there be a way of putting it in the cwd directly in the xcode project?
ennuikiller
@enn: see my edit
sth
If you do a Get Info on your executable in Xcode you will see that you can set the default working directory to anything you like. Having said that, your program shouldn't be making any assumptions about working directory anyway, since it can be set to anything when you're app is launched.
Paul R
yes the program should not make any assumptions about locations, I was just trying to do this for convenience....
ennuikiller
I think its ok if a "hello world" program makes some assumptions that wouldn't be best practice in a real program.
sth
+2  A: 

In your current case out_file would be created even if it doesn't exist (because you're using std::ofstream).

in_file, on the other hand, has to exist, and (I guess there is no such file in the directory with created binary), hence an error is produced.

Did you try launching your compiled application with the file in the same folder where the binary file is?

Kotti
please send my update in the question
ennuikiller
A: 

Turns out you have to specifically create the file here:

Project Path/build/Debug

apparently you can't just define it directly in the xcode project

ennuikiller
+2  A: 
  1. In Xcode, expand the "Targets" node and find your target. Given what I've seen, it'll be the only one there.
  2. Next, right-click on your project and add a new "Copy Files" build phase. When the dialog box comes up, make sure you set the Destination to "Products Directory".
  3. The new build phase will appear beneath your target with whatever name you gave it. Drag your in_file and out_file there.
  4. Compile and run.
Gregory Higley
Thank you Gregory, you've restored my faith in StackOverflow!!
ennuikiller
My pleasure. This is pretty basic Xcode stuff, so I'm surprised that someone hadn't answered it before me!
Gregory Higley
As you can see they would prefer to chastise me for using embedded images!!
ennuikiller
True, but at least you've learned now not to do it. I do think there are rare cases where an image is helpful, but not with plain code.
Gregory Higley
ennuikiller
@ennuikiller To me it's not a big deal about the images. I don't think the size of the images is the problem. The problem is that the images are hosted somewhere other than StackOverflow. ImageShack, to be specific. One of the purposes of SO is to help other people, not just the questioner. Years from now, this question might help another developer with this very problem, but if the images no longer display, it may be harder for that person to figure out what's going on. I try to avoid external resources when using SO.
Gregory Higley
Thank you VERY much for an intelligent answer that makes sense, instead of, like the other responders, just chastising me and down-voting! Now the question I have is why allow embedded images at all then?
ennuikiller
@ennuikiller I agree. I think they should not be allowed.
Gregory Higley