I've been using windows in a class I've been taking but I am trying to run a basic code to figure out how to open/close/input/output from files on Xcode and the code I usually use on visual studios isn't working any idea why? thanks!
#include <string> #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; ofstream fout; string input; fin.open("inputFile.txt"); if(fin.fail()) cout << "File failed to open." << endl; fin >> input; fout.open("outputFile.txt"); fout << input; }
Under Product , right click on the output program(not the product folder), and click Show in Finder , then drag the program into Full Disc Access or Files and Folders from earlier. Worked.
Delete the files from the xcode project navigator (only deleting references). Add them again to the project (dragging from finder into the project navigator).
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... In the dialog that appears, select the Other category and the Empty file option. Name the file numbers. txt.
Put your .txt files in the same directory where your main.cpp file is (or anywhere you like).
In Xcode go to Product > Scheme > Edit Scheme > Run test (on the right) > Options (middle top)
Down under Options check “Use custom working directory” and set it to the directory where you .txt files are located.
To work with the files, you will have to specify just file names, e.g. in_file.open("inputFile.txt"); no path is necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With