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

Calhounian

macrumors newbie
Original poster
Feb 11, 2010
4
0
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!
 
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.
 
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?
 
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.
 
See this past thread:
https://forums.macrumors.com/threads/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.
 
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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.