Xcode 4 C++ Project add file to build directory - c++

I'm new to Xcode. How can I add a file to the build output directory? I have a file.txt which I want to open in C++.
The following code...
string line;
ifstream file ("file.txt");
if (file.is_open())
{
while (file.good())
{
getline (file,line);
cout << line << endl;
}
file.close();
}
... results in Unable to open file.
I found out that the executable is deployed to /Users/myusername/Library/Developer/Xcode/DerivedData/IAIK_CMS_Test-dfmszjneldfwjffsmjpmtiepyzej/Build/Products/Debug/
I've tried to add the files in the project settings to the "Copy Files" in the "Build Phases". For details see this screenshot: http://i.stack.imgur.com/QhkIT.png
Edit: I'm looking for something like Eclipse's "add to Build Path" for Java.
Does anyone have a suggestion?

Xcode5 is out, still no answer :)
For Xcode5 in C++ project settings, choose your app target, select "Build phases" tab, unfold "Copy Files" section, set "Products Directory" for Destination, leave Subpath field blank, uncheck "Copy only when installing" and add necessary file (file.txt) to the list using plus sign, like that:
And use "file.txt" path to access your file.

Related

I am having trouble opening a .txt file in my C++ program

I have put it under the Source Files folder. I have set my working directory to $(SolutionDir)$(Configuration)\ (im not sure if that is right, i saw it somewhere) could someone help me troubleshoot.
int main()
{
cout << "program running" <<endl;
pair<int, unsigned int> mypair;
ifstream myfile;
myfile.open("numbers.txt", ios::in);
if (!myfile.is_open()){
cerr << "can't open input file" << endl;
}
else
{
cout << "file opened" << endl;
}
getchar();
myfile.close();
}
...output is: can't open input file
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
There is such thing as current directory for an application. When you run an app from VS working directory would be current one.
If you specify file name with relative path (or no path at all) OS will try to find that file relative to that directory. Where executable file and especially source file(s) are located completely irrelevant. So solution could be either set working directory to where numbers.txt is located (or move nubers.txt there), or use relative path something like foobar/numbers.txt or even ../foobar/numbers.txt etc or use absolute path.
okay I managed to figure it out..with help :) I did what Paulo M said :
To figure out what directory you're pointing to, try to create a new file instead of opening one:
std::ofstream("out_test.txt");
Then you can find that file searching with File Explorer into the solution dir.
After I figured out where the file got sent I added a new file w/ some integers to that dir and made sure the working directory was the same.I'm not sure if i had to change it? (can files be sent elsewhere?).
I had tried to do this before but I was not able to move my txt file into this directory by copy paste..so gave up. But anyhow I just right clicked/new text document/ and edited the doc with some numbers. then changed my program to open this new document.
saving file directly to the directory also worked. :) :) :)
but i am curious why I couldn't just paste it there?

Why can't I read a file with Code::Blocks C++?

I've created a file in Code::Blocks called datos.csv, and I have this code:
std::ifstream file("datos.csv");
if (file) {
cout << "Managed to read file successfully.";
}else{
cout << "Unable to read file.";
}
But it is unable to read the file.
I tested the same code with TextMate, which can run C++ files, and it was indeed able to read the file, so I suppose there's something up with Code::Blocks. What am I missing?
My file appears listed in "Others" in Code::Blocks' navigator.
you need to modify the Target Properties, go to Project -> Properties -> Build targets and change the "Executing Working Dir" for the debug/release folder of your proyect, I hope this help.
Greetings.
Saludos.
It can't find the file to open it. Since you are not using absolute paths to open the file it must be relative to the current working directory. If you are launching from the debugger you can set the working directory used when the application is launched. Make sure that directory is the same as where the csv file is located.

Code runs perfect in g++ but not in Xcode - Cannot find File

I have created a text file with content. It is located in the same folder as the cpp files. And I have confirmed several times that the file exists. When I run g++, compile and run it finds the file. When I run it in Xcode, it does not work. If fails to find the file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Your file fails to open because XCode launches from the IDE in the default build location, which is actually a temporary directory off somewhere on your disk. If you want change the working directory at launch-time to something else (like the location where your files can be found):
Select the Product/Edit Scheme... menu option.
Select the Run schema in the left list.
At the bottom Options tab on the right pane should be a "Working Directory" option. Check the checkbox and set the custom working directory to someplace you know (your "/Users/yourname" home directory is a decent place that I use).
Make sure any "current directory" data files you need for your program execution from the IDE are in this directory.
And in case you didn't see it, this is also the place where you can configure command-line arguments (on another tab) of the same dialog.
Try to add files to the project in XCode or use the absolute path instead.

Can't read file from a C++ program on a Mac

First of all, this is part of my code:
....
string input;
getline(cin, input);
ifstream openFile;
openFile.open(input.c_str(), ios::in);
if(openFile.is_open()){
cout << "File opened" << endl;
}
else {
cout << "Cant open the file " << endl;
}
The result always "Cant open the file". I am very, very sure that the files are exists. I have data1.txt, data2.txt ... data10.txt in the same directory (I used XCode to add new empty file, add the data inside and save it).
I do another test, I create a new directory, copy paste the cpp and data files. I run in terminal, and it works, it can read the data file. Why does xcode cant read my data files? Any idea?
You need to give the full path to the files. Xcode will run the application from the build directory which is not where the code is.
If the files are copied as part of building an OSX or iOS application you should look at the bundle structure to find the directory.
You can tell Xcode to run the executable from the directory containing the data files.
Bring up the info dialog for the target executable, and change the value for 'Set the working directory to:' to either the Project directory or a custom directory'

Open method opens files with full path only C++

File opens if I write the full path (full-path/roots.txt).
File fails to open if I write the filename only (roots.txt)
And yet, roots.txt is in the same folder as the main.cpp.
Is there any settings I should check on XCode?
Here's the code:
string line;
ifstream infile;
infile.clear();
// infile.open("roots.txt");
infile.open("/Users/programming/C++/roots/roots.txt");
if (infile.fail()) cout << "could not open the file: " << strerror(errno);
getline(infile, line);
cout << line;
By default, Xcode working directory is something like ~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug. So,if you are trying to use a relative path with respect your project directory or any other, you should manually configure your working directory in Xcode.
You can do this by: Product -> Scheme -> Edit Scheme -> options tab, tick the use custom working directory checkbox and show your path.
If it works when you attempt to open a file with an absolute path and fails with just the filename, your relative path is likely incorrect. Ensure roots.txt is placed in the current working directory. Look into the getcwd function declared in unistd.h.
To change the working directory when you're running from inside XCode: select "Edit Active Executable" in your "Project" menu.
You can adjust your working directory settings at the bottom of the "General" section.
Assuming you're running file from within XCode, the working directory is unlikely to be the same directory where your .cpp file is located. Check what your current working directory is what you think it is. (you should be able to obtain it using a getcwd call)