How do I access a .txt file in Xcode for an OSX command line tool?

I'm in school learning C and C++, and we have to read from and write to .txt files regularly. But I can't figure out how to open a .txt file in Xcode.

if FILE *buffer = fopen(filename,"r"); where filename is a pointer to a char array holding the name of the file, it works perfectly fine in the terminal. It compiles, in both Xcode and GCC, but when I try to actually open the file in Xcode, my program can never find it and quits the application (so it doesn't crash).

Long story short, what do I have to do to get my programs in Xcode to read from a .txt file?

I tried putting a copy of input.txt in every folder within the project, including it in the source files, adding it to target (which I don't wholly understand), I don't know what to do.

MacBook Pro, OS X Mavericks (10.9), Quad Core i7 15" 10GB RAM

Posted on Oct 9, 2014 12:31 PM

Reply
3 replies

Oct 9, 2014 1:55 PM in response to andrewbatz

Make sure the filename has an absolute path. If the path is relative it will point to a different location depending on where you run the program from.


When you run it from within Xcode itself the binary is buried way inside the project hierarchy (you can see where it is by selecting the program in the Products folder and doing a Show in Finder).


The safest bet is to always specify a specific directory for any writes you do.


regards


As an example for this program in Xcode (assumoing you will be able to see the images)


User uploaded file


The binary is located here


User uploaded file

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I access a .txt file in Xcode for an OSX command line tool?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.