1

enter image description here

In Xcode, when I fopen a file that doesn't exist in my computer, you can see it created in the directory you choose, but it won't show up in the contents in the left of Xcode. How can I make it show up in the Xcode once I create it?

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
jack
  • 121
  • 9

1 Answers1

0

You need to create a so-called "Folder reference" to your output folder.

Xcode has two kinds of folders in a project tree:

  • Group references
  • Folder references

The difference is explained for example here: Difference between folder and group in Xcode?.


When I use C function "fopen("test.txt", "w+") create a new file "text.txt", it will appear in my directory of main.c, but it won't show up in the left content(project navigator), I mean how I see the "text.txt" appear in the content once I create it, but not add it manually? – 李梧畅 yesterday

For single files Xcode only supports "Group references" which means that a file will not appear automatically if you create it in the physical folder. So you have only two options:

  • Create a "Folder reference" and create your file to that folder as I demonstrate with screenshots below.
  • (stupid, not recommended) create empty file add it as "Group reference" via "Add Files" and then delete the file. In this case you will have your file in Xcode but it will be red if the file does not exist.

Below are the steps needed to accomplish this with screenshots:

  • In the file system, in the folder where your main.c is located, create a folder of given name like Output.

  • In Xcode right-click on your folder practice_io, click "Add Files to ...".

  • Select 'Output' folder. Also click Options, where select "Create folder references", click "Add"
  • You will see blue folder "Output".
  • In your C program make your file to be created in Output directory
  • When you run you C program, the file will be created in that folder and Xcode will see it.

enter image description here

enter image description here

enter image description here

Community
  • 1
  • 1
Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
  • thank you very much, but I don't mean that I want to create a folder in the Xcode. – jack Dec 04 '16 at 01:48
  • When I use C function "fopen("test.txt", "w+") create a new file "text.txt", it will appear in my directory of main.c, but it won't show up in the left content(project navigator), I mean how I see the "text.txt" appear in the content once I create it, but not add it manually? – jack Dec 04 '16 at 01:52