Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dir > List.txt
- dir > list.txt 2>&1
- /*
- Tee utility by VladD
- Use it in whichever way you like
- Hopefully it doesn't do anything bad, but no warranty
- Compiles with MSVC 2012
- */
- #include "stdafx.h" // comment this line out if compiling with non-Microsoft compiler
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <cstdlib>
- using namespace std;
- bool append = false;
- char** curr_arg;
- vector<ostream*> target_streams;
- vector<string> stream_names;
- bool have_open_error = false;
- bool have_io_error = false;
- void evaluate_args()
- {
- for (; *curr_arg; curr_arg++)
- {
- string arg(*curr_arg);
- if (arg == "--")
- {
- curr_arg++;
- break;
- }
- else if (arg == "-a")
- {
- append = true;
- }
- // add more arguments here
- else if (arg.size() > 0 && arg[0] == '-')
- {
- cerr << "Unknown argument: " << arg << endl;
- exit(1);
- }
- else
- {
- break;
- }
- }
- }
- void create_streams()
- {
- auto openmode = ios::out | (append ? ios::app : ios::trunc);
- target_streams.push_back(&cout);
- stream_names.push_back("-");
- for (; *curr_arg; curr_arg++)
- {
- string arg(*curr_arg);
- ostream* p;
- if (arg == "-")
- {
- p = &cout;
- }
- else
- {
- p = new ofstream(arg, openmode);
- if (!*p)
- {
- delete p;
- cerr << "Cannot open file `" << arg << "'" << endl;
- have_open_error = true;
- return;
- }
- }
- target_streams.push_back(p);
- stream_names.push_back(arg);
- }
- }
- void destroy_streams()
- {
- for (auto p : target_streams)
- {
- if (p != &cout)
- delete p;
- }
- }
- void process()
- {
- string line;
- while (getline(cin, line))
- {
- for (size_t i = 0; i < target_streams.size(); i++)
- {
- auto p = target_streams[i];
- if (!p)
- continue;
- (*p) << line << endl;
- if (!*p)
- {
- cerr << "Error occured writing to stream `" <<
- stream_names[i] << "'" << endl;
- delete p;
- target_streams[i] = nullptr;
- have_io_error = true;
- }
- }
- }
- }
- int main(int argc, char* argv[])
- {
- curr_arg = argv + 1;
- evaluate_args();
- create_streams();
- process();
- destroy_streams();
- return (have_open_error ? 0x2 : 0) | (have_io_error ? 0x4 : 0);
- }
- tee [ -ai ] [ File ... ]
- -a Append the output to the files rather than overwriting them.
- -i Ignore the SIGINT signal.
- dir > C:log.txt && dir
- dir >list.txt >&2
- dir >list.txt 1>&2
Add Comment
Please, Sign In to add comment