I am using Mac and Xcode for my following code which should get my cin
value for the name
and the age
and write them in the file fileProcessingTrial.txt
.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
// insert code here...
string name;
int age;
ofstream fileProcessing("fileProcessingTrial.txt", ios::out);
cout<<"Please enter your name: "<<endl;
cin>>name;
cout<<"Please enter your age: "<<endl;
cin>>age;
fileProcessing<<name<<" "<<age<<endl;
return 0;
}
And then where is my file fileProcessingTrial.txt
stored (by default?) if I want to open it? Can I store it in a specific location?
I thought I would add an answer specific to Xcode. If you are building and running the executable in Xcode (the IDE), then the output file (if you did not specify an absolute path for the filename) will go to the same directory as the Build Products because that is where the built executable will be. This becomes the current working directory
mentioned by Jesper Juhl when Xcode runs the executable. To locate that, click on the product in the Project Navigator (in the below screenshot this is the File Out
executable in the left pane). Then look in the File Inspector in the upper right pane. The directory part of the Full Path is where your output file is.
If you did specify a relative path, then the location will be relative to this directory for build products, and as Jesper said, you should avoid encoding an absolute path in your program.
In Xcode, you can also change the current working directory
by editing the scheme:
Hope this helps.