Use this file to help Xcode behave more like a command line shell when it comes to dealing with file redirection. This addresses missing functionality for:
* Input redirection (cin >>): < inputfilename
* Output redirection (cout <<): > outputfilename
* Error redirection (cerr <<): 2> errorfilename
First, download the file directly or use curl
or wget
.
% wget https://gitlab.umich.edu/eecs281/xcode_redirect/raw/master/xcode_redirect.hpp
or
% curl -O https://gitlab.umich.edu/eecs281/xcode_redirect/raw/master/xcode_redirect.hpp
In your Project Navigator or through the File Menu, you can "Add Files..." to your project.
Navigate to the downloaded copy of xcode_redirect.hpp
and add it to the
project with the following settings:
In the main editor window or through the Project Menu, you can "Edit Scheme..." to add command line parameters and file redirections.
For each file redirection, include an entry in "Arguments Passed On Launch." Be sure to start with a redirection (<, >, or 2>), and then include the full absolute path to the file, surrounded in double quotes. Check the boxes to enable or disable various configurations of redirections.
Include the following line inside your project's main()
function, before you
make any reference or use of argc
or argv
:
#include "xcode_redirect.hpp" // Add near the top of the file with main()
...
int main(int argc, char *argv[]) {
xcode_redirect(argc, argv); // Be sure to do this!
...
cin >> my_variable; // Reads from file instead of keyboard when redirected
cout << my_result; // Prints to file instead of screen when redirected
cerr << my_msg; // Prints to file instead of screen when redirected