Mac Xcode and C++ — File Handling

I am glad to keep up and be able to write the third post. If you are coming here for the first time and confused how to start the project in Xcode. You can check my previous post here : Mac Xcode and C++ — Beginning

Talking of File Handling… It can get tricky with Xcode. When I started doing it I was super confused and it took me quite some time to figure out how to do it properly.

Before talking of File handling with Xcode. If you are confused in terms of how of I can write C++ code to handle a file. Google is a great place but for the time being you can check :

or if you are in a hurry : Check my simple tutorial source which will ask you for the File name and then will add your name and age to the file and then will print the whole file :

Now talking of file handling : If you run the program, it will execute and you will be able to enter the name and age and the file will get printed. Run the program multiple times you will feel great and the program will run but one important point “WHERE IS THE FILE??”

You will search the directory and you wont find it. Now create a New file : lets say “myText.txt” and add two lines

“Your Name”

“Your age”

Now run the Program and opps you wont see the above text? Why?

So the reason to the problem is : Xcode and C++ checks the runTime Directory and there is no such file “myText.txt” so C++ creates a new file and if you run the program again since the file is there it will print the file contents!

but Where is this file? 😡 thats how I felt and after searching the solution is :

Go to your Xcode -> Preferences->Location and click on the small arrow right next to Derived Data path. It will open the finder.

If you look at the left image this is what you will see. In the derived data directory you will see your project in that project check build->Products as shown in right file and you will find your files.

Now you can edit the file and thats how you can do File Handling using Xcode but are you happy with this? I was not. I was confused as How I will push the data to my github. How I will manage the text files and how I can share the project!

This made me to make a decision that Xcode will not work for me but then there is more to this.

The Right way to handle files in C++

Click on the Button next to Stop (It will show the name of your current running Target) and you will see Edit Scheme. Click on it and you will see Scheme Menu :

Working Directory is what we are looking here. Click on the CheckBox and set the path of your current working directory to your target path and then add a file “testFile.txt” and add some data to it.

To Manage files and Target you should have a look at :

Run the program and you will see the current file running. You can now see the file changes live in Xcode and its now a part of your project directory which you can push to Github or can copy your project and send it to your teacher or friends.

Thats how you can handle files in C++. If it worked for you Hit a like so that others can know about it :)

I am learning to write better posts.. If you find something better let me know. Hope this helped you :D