0

I am working with Xcode and I am having trouble opening a file stream to assign variables from a text file. I speculate that placing the txt file in the same directory as the project would allow me open the stream without including the entire dir. I have been messing with this for a little while to no avail can I get it to work properly. I believe I was able to read data at one point, but I think the string printed was in unicode (not sure). It is a very simple program.. I would think that it would work. I think my problem has to do with the directory the example is in and how Xcode works with project files. I just put the example file in the project folder and hoped that it would work.

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

using namespace std;
int main()
{
    string name;
    ifstream infile;
    infile.open("example.txt");
    if(infile.is_open())
    {
        infile >> name;
    }
    else
        cout << "Unable to open file";

    cout << name;
    return 0;
}
TEddyBEaR
  • 137
  • 1
  • 9
  • 2
    Does it work with an absolute path? – Borgleader Jun 03 '15 at 20:16
  • I haven't tried that. gave it some though, but I don't really know how to do that on a mac. I recently got this computer and I am not really fond of Xcode yet. – TEddyBEaR Jun 03 '15 at 20:17
  • 3
    How are you running the program? Because you have no path specified on the file, it will check your present working directory which isn't guaranteed to be where the program resides. – Mr. Llama Jun 03 '15 at 20:18
  • I am building it from Xcode. I even imported the file into the project and that didn't work. it was still unable to open the file. where else can the program reside if not the working directory. – TEddyBEaR Jun 03 '15 at 20:18
  • 2
    If you're going to give a relative path to the stream, then the path should be relative to the current working directory. Try printing that out before you attempt to open the given file, you may see that your relative path is off. The current working directory is usually the same directory as the executable you run from, but that could get changed at any time (e.g. during initializzation any third party library). This is why Borgleader asked about absolute paths; by their nature you shouldn't need to modify them depending on the current working directory. – AndyG Jun 03 '15 at 20:20
  • It's in the working directory. how do I print the relative path. I am pretty sure that Xcode manages the projects differently than visual studio. I would imagine that importing the file to the project would have worked though. – TEddyBEaR Jun 03 '15 at 20:22

1 Answers1

3

First of all, remember, that working directory is not always the same directory where the program's binary resides.

Change:

infile.open("example.txt");

to:

infile.open("/full/path/to/program/directory/example.txt");

where /full/path/to/program/directory/ is the location of folder, where program (and thus example.txt file) is placed. It should fix the issue.


By the way, you may also want to read this question, that addresses very similar problem.

Also, read about getcwd() function.

Community
  • 1
  • 1
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49