Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Program in Xcode not outputting simple text file using outFile

I am running a program using this simple example code to output a text file. I am using Xcode and simply started a new C++ project from command line tools. For some reason the program does not output any file onto my Mac. Please help figure out why XCode will not output any file to my computer? Thanks!

#include <iostream>
#include <fstream>

using namespace std;

int main() {
double myNumber = 42.5;
fstream outfile("test.txt", fstream::out);
outfile << "The answer is almost " << myNumber << endl;
outfile.close();
}
like image 565
Nearpoint Avatar asked May 27 '13 19:05

Nearpoint


People also ask

How do I open a TXT file in Xcode?

txt file is not in the project folders, but in the same folder as the compiled program. You can find this by right selecting the executable under the 'Products' folder in the left pane of Xcode and selecting "Show in finder" and then move your . txt file there, and run it again.

How do I add a text file in Xcode C++?

Since our first example program needs to work with a couple of text files we will need to create those and add them to our project. To add a text file to the project, right-click on the project folder in the project view in the left pane and select the option New Item...


2 Answers

Dang I can't believe I figured it out, it was an option in Xcode. So I clicked on the bar at the top of xcode near the stop button with the text (Project Name > My Mac 64-bit)

Then clicked edit scheme. The clicked on the options tab and clicked use custom working directory. Then selected a working directory. Now the text file appears!

like image 190
Nearpoint Avatar answered Oct 10 '22 22:10

Nearpoint


As mentioned above, editing the scheme worked for me but getting to the scheme was different.

  • Use the Product Menu
  • Select the Scheme option
  • Select the Edit Scheme option
  • Click the Options Tab
  • Tick the "Working Directory" item
  • Click the small icon at the right end of the text box.
  • Select the directory

I also had the Run/Debug option selected in the left hand pane of the window.

like image 36
peterdcasey Avatar answered Oct 10 '22 20:10

peterdcasey