Changing directory for input/output files in Xcode 3.2

Discussion in 'Mac Programming' started by Calhounian, Feb 11, 2010.

  1. Calhounian macrumors newbie

    Joined:
    Feb 11, 2010
    #1
    Hello all,

    I am writing a C++ program using Xcode 3.2. I need the program to open an input file and then eventually output a file. I have included <fstream> in my project and have done all of the setup correctly. I realized that my file is not being opened because it was in the wrong directory. I placed it in the ../build project directory with the target file, but it is not being read in that location. When I output a file, it is created in my User folder directory. Is there a way to change the settings in Xcode so that it will read files from and output files to the build folder within the project directory (in the same location as the executable)?

    Any help would be much appreciated! Thanks!
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Project Menu->Edit Active Executable

    Click the "General Tab" and make your choice from the "Set the working directory to:" radio group.
     
  3. Calhounian thread starter macrumors newbie

    Joined:
    Feb 11, 2010
    #3
    I have that set to Project Directory (which was the default), and still no luck. I also tried using a custom path to the same build folder within the project directory and it did not work.
     
  4. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    It works without failure in every case for me.
     
  5. Calhounian thread starter macrumors newbie

    Joined:
    Feb 11, 2010
    #5
    Are you running 3.2.1?
    I just tried it again with a brand new project, and it still is not reading/writing to the right directory. It shouldn't matter if I am running in Debug or Release, right?
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    You'll have noticed it says "Edit Active Executable <Target Name>", so it should apply to all project level configurations as there is no way to specify otherwise.

    Xcode version shouldn't make a difference. But to answer the question: I was until last night.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    See this past thread:
    http://forums.macrumors.com/showthread.php?t=796818

    Google search terms:
    site:forums.macrumors.com C++ fstream
    Other options:

    Add code to your program to print its working directory. The C functions are getcwd() and getwd().

    If it still doesn't work, pass a command-line arg with an absolute pathname.

    If an absolute pathname doesn't work, post compilable source for a simple fail-case that exhibits the problem.
     
  8. Calhounian thread starter macrumors newbie

    Joined:
    Feb 11, 2010
    #8
    Thanks for the advice, but I still can't seem to get it to read/write to the project directory, even though the active executable is set there.

    Here is the code I'm using:
    Code:
    #define _GLIBCXX_FULLY_DYNAMIC_STRING 1
    #undef _GLIBCXX_DEBUG
    #undef _GLIBCXX_DEBUG_PEDANTIC
    
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    	cout << "Enter the name of an input file: ";
    	string inputFile;
    	cin >> inputFile;
    	
    	ifstream infile(inputFile.c_str());
    	bool fileOpen = infile.is_open();
    	cout << "File is open: " << boolalpha << fileOpen << endl ;
    	
    	while(!infile.is_open())
    	{
    		cout << "File did not open!" << endl;
    		cout << "Please enter a valid filename (or QUIT to exit): ";
    		cin >> inputFile;
    		if(inputFile == "QUIT")
    		{
    			return 1;
    		}
    		else 
    		{
    			ifstream infile(inputFile.c_str());
    		}
    	}
    	
    	infile.close();
    	
    	return 0;
    }
    
     

Share This Page