PDA

View Full Version : Xcode 4.6. Can't open .txt file




thrill4rishabh
Mar 20, 2013, 03:57 PM
Hi, I am a beginner C++ programmer, beginner Mac user. I cannot open text files for my project, so I tried to run a simple program to read data from a file and it doesn't work.

I have tried placing the file with the executable, tried placing it with the project file and even tried using the full path and it doesn't work. I'm using Xcode 4.6 by the way.

Please help.



#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
cout << "opening data1.txt....\n";
ifstream infile("Data1.txt");
int n=0;

if (infile.is_open())
{
while (infile >> n)
{

cout << n;

cout<<"\b";

}
infile.close();
}
else
{
cout<<"ERROR";
}
return 0;
}



chown33
Mar 20, 2013, 04:29 PM
First, you need to understand what a working directory is:
http://en.wikipedia.org/wiki/Working_directory

Second, you need to find out what the working directory is when the program runs. To do that, use Xcode's builtin documentation to look up the C function getcwd(). You can also find references for that function on the internet.

Third, if you tried a full path, post the exact code for it. Otherwise we're just guessing what you wrote, and you're the only one who knows what was really coded.

Fourth, Xcode can set the working directory for running a program. See here:
http://forums.macrumors.com/showthread.php?t=1285568

Or google search terms: xcode working directory.

ArtOfWarfare
Mar 20, 2013, 04:37 PM
I can't remember the exact spot but there's someplace in your project settings where you define which files should be copied during a run. Add to that list that it should copy the files you want to read into the product directory.

thrill4rishabh
Mar 20, 2013, 05:05 PM
Actually I got it to work. Thanks.

SableRaph
May 28, 2013, 11:41 AM
Actually I got it to work. Thanks.

Would you mind sharing how you got this working?

chown33
May 28, 2013, 12:26 PM
See post #2:
Fourth, Xcode can set the working directory for running a program. See here:
http://forums.macrumors.com/showthread.php?t=1285568

Or google search terms: xcode working directory.

The first result from a google search is a StackOverflow article with this:
Just updating this solution to XCode 4.x

You go on Product -> Scheme -> Edit Scheme

On the tab Options you choose your working directory.

SableRaph
May 28, 2013, 01:22 PM
Actually, that topic solved my problem : http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c

In case the link ever dies :


#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif

// ----------------------------------------------------------------------------
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
// error!
}
CFRelease(resourcesURL);

chdir(path);
std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------