Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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.

Code:
#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;
}
 
Last edited:
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:
https://forums.macrumors.com/threads/1285568/

Or google search terms: xcode working directory.
 
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.
 
See post #2:
Fourth, Xcode can set the working directory for running a program. See here:
https://forums.macrumors.com/threads/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.
 
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
// ----------------------------------------------------------------------------
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.