Creating a file in C ++ on Xcode

It is supposed to create a file in my project directory called "tuna.txt". When I run it, it compiles successfully, however the file is not created. I am using mac using xcode. I was looking for my computer for other places where it could be created, but it looks like the file was not created at all. Any ideas as to why this is not working?

#include <iostream> #include <fstream> using namespace std; int main(void){ ofstream file; file.open("tuna.txt"); file << "I love tuna and tuna loves me!\n"; file.close(); return 0; } 
+4
source share
3 answers

I assure you that files are created that prohibit errors (which you do not check). Xcode tends to use the final build-dir as the current working directory when working from the IDE. You can change this by editing the active schema.

  • Click the Project button to the right of the STOP button on the main toolbar
  • Select "Edit Schema"
  • Select the Run subroutine in the list of the left pane.
  • Select the Options tab,
  • Check "Use custom working directory"
  • Set the working directory to a known location (for example, the project root folder).

Note. Here you can also configure any command line arguments (they are on the "Arguments" tab, not the "Options" tab) if you want to do this.

+10
source

First of all, you should check if the file was opened/created or not. Then you should search for the file. Most likely, the file has not yet been created. Here is the code:

 #include <iostream> #include <fstream> using namespace std; int main(void){ ofstream file; file.open("tuna.txt"); if(file.is_open()) { file << "I love tuna and tuna loves me!\n"; file.close(); } else cout<< "No file has been created!\n"; return 0; } 

As you did not specify the absolute path to open the function. Look at the folder where your code file is located. Most likely the file will be there.

+1
source

In the Products folder (in the Project Navigator on the Navigator tab on the left side of the Xcode IDE) you will find the executable file. Click on the executable file.

If you haven't already specified, make sure the Show File Inspector icon is displayed on the Utilities tab to the right of the Xcode IDE.

From the inspector you will see the full path showing the path to the executable file, and at the end there will be an arrow. Clicking on this arrow will open the Finder window in this place, and here you will also see all text files and other files created from the program.

PS. The reason the tuna.txt file was not found when using the search is because it is located in a hidden folder with the executable file.

0
source

All Articles