Xcode writing to file issue - c++

I'm trying to help my son with a Programming C++ for Engineers course. I got him set up with Xcode 13.1 on his iMac but I've never used it myself. That was working great until his first assignment that required writing output to a file. We created a new Command Line Tool project and then used the File->New->File... option to create an Empty file. After doing so, selecting that file and looking at the info on the right side of the window shows that the Full Path is pointing to the directory where his main.cpp file is. Yet, when running his code nothing was being written to the file. I had assumed that just referencing the file name in his code would cause it to use a local reference and look in the same directory as main.cpp. After much Googling I found a reference that said to go to Product->Scheme->Edit Scheme... then select the Options tab in the window that opens and change the Working Directory from "$(BUILT_PRODUCTS_DIR)" to the directory containing the main.cpp file. That got the programming working correctly. My questions are:
Is there a preference setting that would tell it to always use the current project directory so that we don't have to change this every time?
Where does "$(BUILT_PRODUCTS_DIR)" point to?
What should we be doing differently?

Related

New to Xcode can't open files in c++?

I've been using windows in a class I've been taking but I am trying to run a basic code to figure out how to open/close/input/output from files on Xcode and the code I usually use on visual studios isn't working any idea why? thanks!
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
string input;
fin.open("inputFile.txt");
if(fin.fail())
cout << "File failed to open." << endl;
fin >> input;
fout.open("outputFile.txt");
fout << input;
}
Put your .txt files in the same directory where your main.cpp file is (or anywhere you like).
In Xcode go to Product > Scheme > Edit Scheme > Run (on the left) > Options (middle top)
Down under Options for "Working Directory" check “Use custom working directory” and set it to the directory where you .txt files are located.
To work with the files, you will have to specify just file names, e.g. in_file.open("inputFile.txt"); no path is necessary.
Here's a completely different approach: Have Xcode copy the input file for you.
Select your project in Xcode
Select Build Phases
Click the '+' button to create a new Build Phase
Select New Copy Files Build Phase
Select Products Directory
Click the '+' button to add your file
Click Add Other
Select your input file and click Open
Check the Copy items… checkbox and click Finish
Now every time you build your project, the input file will be copied to the same folder as the executable no matter where it is built. Of course, to see the output file, you'll still need to find the executable in Finder.
The answers don't really explain the problem so I thought I'd do that.
When you pass a relative path like "inputFile.txt" to file APIs, it's treated as relative to the working directory when the program is executed. This is the same as the 'working directory' when you use cmd.exe or Terminal.app or command lines in general. The Unix command pwd ("print working directory") displays the current working directory. On Windows running the command cd with no arguments performs the same function. (On Unix running cd with no arguments will change the working directory to the user's home directory.)
When you run a program from the command line, the command line shell sets the program's working directory. When you run a program from within an IDE, the IDE sets the working directory. Since, unlike on a command line, there's no obvious answer for what the IDE should set as the working directory, Visual Studio and Xcode set the working directory to different locations by default: Visual Studio sets the working directory to $(ProjectDir), the directory containing the Visual Studio project file; Xcode sets the working directory to the build products directory, i.e. the location the executable was written to.
Some possible solutions to your problem are:
Do not use a relative path, and therefore don't depend on the working directory. This isn't much help in making the program more portable, because the absolute paths will also differ between platforms, and so you will still have to 'configure' the program for each platform. In fact using an absolute path is worse, because it means your source code must differ, whereas it would be better to keep that difference confined to each platform's build configuration.
Configure the IDE to use your desired working directory. Visual Studio can be configured by right clicking the project, selecting Configuration Properties > Debugging > Working Directory, and setting the working directory to the desired path (potentially using Visual Studio build variables).
nepete's answer describes how to configure the working directly set by Xcode.
Configure the IDE's build process to copy your data files to an appropriate location. In Visual Studio you would do this in a C++ project by configuring the project's Properties > Configuration Properties > Build Events.
SSteve's answer covers how to configure additional build steps in Xcode.
I'm guessing you have inputFile.txt in the folder that contains your source code. That's not going to work. You need to put it in the folder that contains the generated executable. To find that folder, right-click on your app under Products and select Show In Finder.
This image shows what it looks like for a command line program. It also shows the Finder window that was opened. As you can see, it is a different folder than the one containing the source code.
As suggested by nepete, edit the scheme, but use $PROJECT_DIR as the custom working directory. Helps with moving the project around, or working in two different environments (e.g., home and office).
BTW. $PROJECT_DIR is one of the Xcode Environment Variables, and also helps with passing file names as command line arguments to programs (settable under "Arguments" in the scheme).
I've struggled with the same problem today. I wanted to add C code to my Swift project and my file pointer was always NULL.
Unfortunately, in XCode 9 for iOS app, I couldn't change the working directory. Changing Build phases didn't help me either. After 4+ hours of trial and error, that's what I've come up with finally and it works:
when copying files to XCode, I've chosen "Create groups", but I needed to choose "Create folder references":
I created a new objective-c file (.m) and copied all my C code there.
I left untouched .h files (XCode generated bridging header and my own .h file with public functions declaration). Now my project structure looked like this:
In my dict.m file in place of previous plain c fopen part:
FILE *dic = fopen("dictionary.txt", "r");
I added obj-C code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"dictionary" ofType:#"txt"];
FILE *dic = fopen([filePath cStringUsingEncoding: NSUTF8StringEncoding], "r");
And it works now without any problem! It's just amazing!
ps I decided to write this answer in case it will help someone like me and will save them some time. If you know how to change working directory in XCode 9 for iOS, please, leave me a comment - now I am really curious why I can't find it.

Changing name of application file (.C file)

I changed the name of an modified application to seperate it from the basic application. Lets say the .C file of the old application is "OldApplication.C" and of the new modified one is "NewApplication.C" Unfortunately, when trying to start the new modified application in terminal by typing "NewApplication" I get:
NewApplication: command not found
Two Question:
How can I make my new application executeable?
Can someone give me a brief overview about the files of a project/application and their functions?
greetings, streight
The mistake of this kind
make: * No rule to make target OldApplication.dep', needed byMake/linuxGccDPOpt/dependencies'. Stop.
often occurs in situation when the Makefile ceases to comply with the structure of the whole project.
In your case make script obviously trying to find the file that was renamed by you and therefore it fails with en error. I think that you should explore the structure of your Makefile and replace all the changed file names with its new versions.
Also you say the you are refused to launch your new executable, but what else could be expected since the project was not compiled?

c++ boost library cannot open file

I tried to work with the boost library to read/write configuration files but I just don't get it.
I even can't run the example code from boost.org (5 Minute Tutorial)
http://www.boost.org/doc/libs/1_49_0/libs/property_tree/examples/debug_settings.cpp
I've downloaded the boost_1_49_0.zip package and unzipped it to my c++ program folder. The code compiles (TheIDE - U++) but it always says "Error: debug_settings.xml: cannot open file" which basically means that the program works, but runs into the exception.
I didn't change the code, I just copy and pasted it to get a working example which I could try to understand then. But I don't even get this one to work. (Since it's exactly the same as in the link, I don't paste the code here... unless you think it's better.)
Please help me... or point to a different way to store variables in a file with some kind of structure (I wan't to learn a way that works for windows and linux, because some of my apps are cross-platform.)
Thanks.
EDIT: debug_settings.xml is in the same folder as the .cpp file
EDIT2: Working now, the debug_settings.xml is now in the folder where the executable is stored. (in my case, U++/TheIDE it's C:\upp\out\MyApps\MINGW.Debug.Debug_Full.Sse2 for debugging)
The configuration file would need to be in the working directory of the executable when it's running.

XCode c++ file reference

I'm trying to write a few algorithm programs practising for Google Code Jam but I can't get XCode to read from an input file (easily).
I have added the file to the project and it appears in the left hand file navigation bar. But I can't reference it relatively. In Code::Blocks I just input from "FileName.txt" and it works but with XCode it just can't find it.
If I put the full file path (i.e. /Users/myname/Documents/Google Code Jam/ProjectName/ProjectName/FileName.txt) then it works but that just seems ridiculous.
Is there anything I can do to get XCode to work? When I added the file I ticked the copy file into project directory and also the add to target executable.
Any help is appreciated.
Thanks!
I just found this and the comments by Julio Gorgé is just what I needed. The default working directory when running in debug mode is some esoteric folder (as you can see in iOS if you include system("pwd"); at the beginning of your main().
It's in the "Options" tab of the Edit Scheme... menu that you can tick "Use custom working directory" and find the test file you put in in the Arguments tab.

C++ program infile won't open in xcode?

alt text http://img638.imageshack.us/img638/5731/screenshot20100613at121.png
Why does the c++ program produce the error shown? I'm especially confused since outfile opens without error yet infile displays the error? Both are defined in xcode exactly the same!! I've altering the "path type" setting without success. The open on infile always fails! Any suggestions would be very much appreciated!!
For those who responded thanks but as you can see but infile and outfile exist and are in the same location:
alt text http://img709.imageshack.us/img709/9316/screenshot20100613at123.png
In Xcode, expand the "Targets" node and find your target. Given what I've seen, it'll be the only one there.
Next, right-click on your project and add a new "Copy Files" build phase. When the dialog box comes up, make sure you set the Destination to "Products Directory".
The new build phase will appear beneath your target with whatever name you gave it. Drag your in_file and out_file there.
Compile and run.
In your current case out_file would be created even if it doesn't exist (because you're using std::ofstream).
in_file, on the other hand, has to exist, and (I guess there is no such file in the directory with created binary), hence an error is produced.
Did you try launching your compiled application with the file in the same folder where the binary file is?
Probably there is no file named "in_file" in the program's working directory, so it can't be opened for reading.
For outfile this doesn't matter since it is opened for writing and if it doesn't exist yet it will just be created.
The directory listing you posted doesn't show where the compiled executable is, but probably it will be somewhere in the build directory. Probably this is then also its working directory and the place where the input file would need to be. (Look for the out_file the program creates when it is run, it will be created in the working directory, the same directory where it searches for in_file. And it is not the directory you posted the listing of, the out_file there is too old.)
Turns out you have to specifically create the file here:
Project Path/build/Debug
apparently you can't just define it directly in the xcode project