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

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();
}

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!

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.

To find the current working directory, in the project navigator, under the "Products" folder, right-click the product and choose "Show in Finder". In this directory you will also find the compiled code.
For example:
~/Library/Developer/Xcode/DerivedData/<project name>-gweghgfqkjkidjfhcgetdryechjz/Build/Products/Debug

Related

compile single c++ source file in 1 project in visual studio

i know a lot of people asked this question, but i can't find how to do it. Is there
a way to build only one source file in visual studio 2017? without new project, i'm learning c++, so i can't make huge thing now, just focus to code(now i'm learn data structure and algorithm),most of my exercise is about <200 code lines, so it great to compile new file without whole project, sometimes i need a few lines of code to test my algorithm,please help me, thanks all you guy, because v.s is very good ide so i want to stick with it.
If you just have one file and want to build it without waiting 1-2 minutes for the IDE to pop up,
Find the Developer Command Prompt in your list of applications - it is under the Visual Studio directory in the Application menu.
cd /d to your directory. cd will take you here if you are on the same drive as visual studio. If you are on a different drive, use cd /d.
Use your favourite editor (notepad, vim, geany, notepad++, nano, microemacs etc) to create the file.
cl sourcefile
Run the excutable.
Unlike what visual studio does, you executable will now be in the same directory as your source. Editors like geany have a build button (the brick icon). All you need to do is fill in how to build: in this case, the cl command.
If you want a one file project, just follow these steps.
Create New Project - File -> New -> Project
Fill in filename, select Win32 Console Application. Note the directory - if it is not where you want it, change it. Click OK
Application Wizard pops up, click Next
Application settings - select Empty project, click Finish
Open Solution Explorer. Right click Source Files. Menu pops up, select Add -> New Item
Add new item dialog pops up, fill in your filename.
If you don't know how to create a new project and a new solution, it will be good to learn those basic concepts and use them to write, test, and debug your code.
You can use one Visual Studio project to do all the learning.
Let's say you want to test "algorithm 1". Then,
Create a header file for it and a source file for it -- call them "test-algorithm-1.hpp" and "test-algorithm-1.cpp".
Add them to the project.
#include the header file in the main .cpp file of the project.
Call the function to test "algorithm 1" from main.
#include "test-algorithm-1.hpp"
int main()
{
test_algorithm_1();
}
When you are ready for testing "algorithm 2", repeat the above steps. The main .cpp file can now be.
#include "test-algorithm-1.hpp"
#include "test-algorithm-2.hpp"
int main()
{
test_algorithm_1();
test_algorithm_2();
}
If you want to avoid testing "algorithm 1" while testing "algorithm 2", simply comment out the corresponding line in main.
int main()
{
// test_algorithm_1();
test_algorithm_2();
}
On the source file you don't want to be included in the project, simply right click, select Properties. There you will find in General a field 'Excluded From Build'. Type true/yes there and the source file will be deactivated.

Why isn't my file opening, C++? XCode and Visual Studio

This is happening in both Visual Studio and XCode
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream input;
input.open ("matrices.txt");
if (!input.is_open()) {
cout << "not open!";
} else {
cout << "open!";
}
input.close();
return 0;
}
My file is not opening. The text file matrices.txt is in the same directory.
What is "same directory" in this context?
For a file to open like this, it has to be in the working directory of the executable when run in the debugger.
For Xcode; this means adding the files to the Copy Files build phase (accessible by editing the executable's target); and adding it to the Products Directory, selected from the dropdown.
In Xcode if you want to work with a file you have to put it in a folder called DeriverData. You can navigate to it by doing the following:
Xcode > preferences > click on Locations.
this brings up the following window
you can click on the small arrow to the right of the path: /Users/dulybon1/Library/Developer/Xcode/DerivedData
It will open the directory containing a list of all your Xcode projects. Find your project and navigate to: build > products > debug
as seen below:
Then you can put your files there. "a1.txt" and "a2.txt" are files that I have put there to work with. And I think the file needs to exist before it can be opened, so just create blank files if you are planning of using them. Hope that helps

"launching 'project' has encountered .." , project file does not exist

I wanted to write 'hello world' in eclipse c++, but it does not work
I go to Run configurations, what config options for c++ programs should i give?
I know I don't care about 'debug' - only 'release', but how to do that?
Here is what I did:
File->New->C++ Project
You will get a pop up window. Type the name of the project you want. Then, below it says executable and inside this folder, I have (by default I guess) Empty Project.
Then click Next and Finish.
Now the project appears in the left column of Eclipse. I right click it and select New->File and name it main.cpp
The main window of Eclipse opens the file main.cpp and I write inside:
#include <iostream>
int main() {
std::cout << "Hello Erjan\n" << std::endl;
return 0;
}
Then I click on Build, it's the hammer icon in the middle of the toolbar. The code compiles and we are ready to launch it!
So, click on Run icon (3 positions right of the Build icon) and you should see the output in the console.
The first step, create new project and try proper toolchains.
Second step, Project >> build project.
Third step, right click on new executive file and run or debug it.

File creation in C++ on Xcode

This is supposed to create a file in my project directory called "tuna.txt". When I run it, it compiles successfully, however no file is created. I am on a mac using xcode. I have searched my computer for other places where it might have been created, but it seems as if the file was not created at all. Any ideas as to why it doesn't work?
#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;
}
I assure you that barring errors (which you're not checking for) a file is created. Xcode has a tendency to use the final build-dir as the current working directory when running from the IDE. you can change this by editing the active Scheme.
Click on the Project box to the right of the STOP button on the main toolbar
Select Edit Scheme
Select the "Run" sub scheme in the left pane list.
Select the Options tab,
Check the "Use Custom Working Directory" checkbox
Set the working directory to some place you know (like your project root folder).
Note: This is also where you will setup any command line arguments (those are on the Arguments tab, not the Options tab), should you desire to do so.
In the Products folder (in the Project Navigator of the Navigator tab on the left-hand side of the Xcode IDE) you will find the executable. Click on the executable.
If not already shown, make sure the Utilities tab on the right hand-side of the Xcode IDE is shown and the Show the file inspector is selected.
From the inspector, you will see Full Path showing the path to the executable, and at the end of it, there will be an arrow. Clicking on this arrow will open up the Finder window to that location, and this is where you should also see all the text files and other files that have been created from within the program.
PS. The reason that you couldn't find the tuna.txt file when using the search is because it is in a hidden folder along with the executable.
First of all you must check whether file has been opened/created or not. Then you should search for the file. Most probably the file hasn't been created yet. 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 haven't given an absolute path to open function.See the folder where your code file is. Most probably the file will be there.

Where does Xcode create .txt files to?

http://www.cplusplus.com/doc/tutorial/files/
I just finished executing this but I didn't get any file on my desktop.
So where does the .txt file get placed on my computer?
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Damn... I've seen this post never answered many times..
Here's the solution
In your project navigator when you're working in your current project there are many files. Try products (if I remember) and look for the executable file. Now go to the properties inspector or whatever is called. (Right side of your Xcode.)
There you will find somewhere a part called PATH the path that is written over there. It's the path where the executable runs. That means there is where you'll find all the files you create with your program.
Try it... that's how I manage and look my .txt files since sometimes I wanna give them some kind of formatting.
I hope I've solved your problem... cheers! ;)
Click on the executable in the Products tab on the left hand side, and you should see the path to it displayed on the right in the utilities section. This is where they are saved. On my computer this path is:
/Users/myName/Library/Developer/Xcode/DerivedData/CurrentXcodeProject-adsnvetbleazktcgitfymfcbhkkt/Build/Products/Debug/filename.txt
In the "current directory", meaning the working directory of the environment from which you ran the executable.
I can't tell you what that is, but in basic cases it may be the directory where the executable is located. In many other cases it may not be.
Performing a search for the file on your hard drive will reveal its location to you.
Right click on your product and select "Show in Finder". Thats opens the path to where the output files go. Also you can add input files there to be read in by your program.
Found best answer here:
File creation in C++ on Xcode
Basically you can specify a known directory so that all files created go there.
If you are on Mavericks Right click in your user folder with your documents and pictures and stuff (~/Users/yourusername)
Now right click and view options. Check "show library". Now follow to the path that the first user above said.
What I've always done when an application saves a file and I don't know where... is just go back to that application, and hit File>Save As...
So re-open Xcode and go to the "File" menu, and click "Save As..."
It will show you the same directory it just saved to.
I have checked the "Current Directory" but I believe I have to specify the location. Not even
~User/Desktop
will work.
What I do is create an empty file with a the correct name on my desktop then drag that file into my Xcode project to get its path. I then delete the file.
Same with adding files to my project. I can add them from the File menu, but I will need to drag them in to the spot in my code where I want to reference them.
It is kind of convoluted but it is the only system I have.
Some of the answers are confusing. Here's the simplest solution.
1. Expand your Products folder on the left-hand side
2. Right click on the terminal icon(sorry I do not know what it's called)
3. Click show in Finder.
You should be able to see your output file in there.
" Where does Xcode create .txt files to? "
You can decide where you want: follow these->
" Product->Scheme->EditScheme "
chose options tab in the new popup "options"
"working directory" tick "use coatroom working directory" and locate the folder where you want.
The .txt file that you "create", or that you want too "read" has to keep in this folder.
In 2022, click on the project name in the "breadcrumbs" area above the editor. Select the correct thing in "Products", then in the right area, see "Full Path".