Wednesday, September 10, 2014

Output the source code of your file.

Here I output the source code for my program to the standard out descriptor.

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

template <class OutIter>
OutIter copy_file (const std::string &file_path, OutIter out) ;

int main ()
{
    std::string const path = __FILE__ ;
    copy_file (path, std::ostream_iterator <char> (std::cout)) ;

    return 0 ;
}

template <class OutIter>
OutIter copy_file (const std::string &file_path, OutIter out)
{
    std::ifstream file ;
    file.open (file_path) ;

    if (!file.good ()) {
        return out ;
    }

    file.unsetf (std::ios::skipws) ;

    auto begin = std::istream_iterator <char> (file) ;
    auto end = std::istream_iterator <char> () ;

    auto new_out = std::copy (begin, end, out) ;

    return new_out ;
}

No comments:

Post a Comment