What would be a correct C++ style for function loading file contents? bool loadFileContents(const std::string&amp; fileName, std::string &amp;out) {
    bool res = false;
    FILE* file = fopen(fileName.c_str(), "r");
    char buf[128] = { 0 };
    if (file) {
        while (fread(buf, 1, sizeof(buf), file))
            out += std::string(buf);
        res = true;
    }
    fclose(file);
    return res;
}


The rationale behind using std::string for the out parameter is that it handles the buffer underneath. However, I feel like it's a move from C-style, but not quite a C++ one.

Should I consider using some kind of stream for the out param?
Here are some of the C-style features of your code:


FILE  
fopen  
char buf [128]   
fclose  


I personally liked the answer that was given in @Jamal's link.

std::ifstream fin(fileName);
std::ostringstream oss;

oss &lt;&lt; fin.rdbuf();
out = oss.str();


The only problem with this is that it doesn't work well with binary files.
You could make out be a std::vector &lt;char&gt; instead.

std::vector &lt;char&gt; loadFileContents (const std::string &amp;filename)
{
    std::ifstream ifile ;
    ifile.exceptions (std::ifstream::badbit | std::ifstream::failbit) ;

    std::vector &lt;char&gt; vecBuffer ;
    typedef std::vector &lt;char&gt;::size_type size_type ;

    try {
        ifile.open (filename) ;
        ifile.seekg (0, ifile.end) ;
        size_type size = static_cast &lt;size_type&gt; (ifile.tellg ()) ;
        ifile.seekg (0, ifile.beg) ;
        vecBuffer.resize (size) ;
        ifile.read (&amp;vecBuffer [0], size) ;
    }

    catch (std::ios_base::failure &amp;e) {
        std::cerr &lt;&lt; e.what () &lt;&lt; std::endl ;
    }

    return vecBuffer ;
}


NRVO allows us not to worry about returning the std::vector &lt;char&gt; by value.  

If you have access to C++11, then you could do:  

std::vector &lt;char&gt; loadFileContents (const std::string &amp;filename)
{
    std::ifstream ifile ;
    ifile.exceptions (std::ifstream::badbit | std::ifstream::failbit) ;

    std::vector &lt;char&gt; vecBuffer ;

    try {
        ifile.open (filename) ;
        std::istreambuf_iterator &lt;char&gt; iter (ifile) ;
        std::istreambuf_iterator &lt;char&gt; eos ;
        vecBuffer = std::move (std::vector &lt;char&gt; (iter, eos)) ;    
    }

    catch (std::ios_base::failure &amp;e) {
        std::cerr &lt;&lt; e.what () &lt;&lt; std::endl ;
    }

    return vecBuffer ;
}


If you want to enter template territory, then you could try this:

template &lt;typename Container&gt;
Container loadFileContents (const std::string &amp;filename)
{
    std::ifstream ifile ;
    ifile.exceptions (std::ifstream::badbit | std::ifstream::failbit) ;

    try {
        ifile.open (filename) ;
        std::istreambuf_iterator &lt;char&gt; iter (ifile) ;
        std::istreambuf_iterator &lt;char&gt; eos ;
        return Container (iter, eos) ;  
    }

    catch (std::ios_base::failure &amp;e) {
        std::cerr &lt;&lt; e.what () &lt;&lt; std::endl ;
    }

    return Container () ;
}


Then in your main, you could do:

int main (void)
{
    std::string strData = loadFileContents &lt;std::string&gt; (FILENAME) ;
    std::vector &lt;char&gt; vecData = loadFileContents &lt;std::vector &lt;char&gt; &gt; (FILENAME) ;

    return 0 ;
}


If we choose to not return the Container, then we could do:

template &lt;typename Container&gt;
void loadFileContents (const std::string &amp;filename, Container &amp;container)
{
    std::ifstream ifile ;
    ifile.exceptions (std::ifstream::badbit | std::ifstream::failbit) ;

    try {
        ifile.open (filename) ;
        std::istreambuf_iterator &lt;char&gt; iter (ifile) ;
        std::istreambuf_iterator &lt;char&gt; eos ;
        container = Container (iter, eos) ; 
    }

    catch (std::ios_base::failure &amp;e) {
        std::cerr &lt;&lt; e.what () &lt;&lt; std::endl ;
    }
}

int main (void)
{
    std::string strData ;
    std::vector &lt;char&gt; vecData ;

    loadFileContents (FILENAME, strData) ;
    loadFileContents (FILENAME, vecData) ;

    return 0 ;
}


The benefit of this one is that we wouldn't have to supply template arguments in main.