views:

733

answers:

7

I need a quick easy way to get a string from a file in standard C++. I can write my own, but just want to know if there is already a standard way, in C++.

Equivalent of this if you know Cocoa:

NSString *string = [NSString stringWithContentsOfFile:file];
+2  A: 

The standard C++ library doesn't provide a function to do this.

Greg Hewgill
Best answer. Sure, it CAN be done, but it's a mess. Much better to accept it isn't there and write your own. A single function call will definitely be a one-liner.
Daniel Straight
+2  A: 

Best I can do is 5 lines:

#include <fstream>
#include <vector>
using namespace std;

ifstream f("filename.txt");
f.seekg(0, ios::end);
vector<char> buffer(f.tellg());
f.seekg(0, ios::beg);
f.read(&buffer[0], buffer.size());
Adam Pierce
Is the f.seekg() trick always correct about the length of the file? What about CRLF conversion on Windows?
Adam Mitz
I guess you could open the file in binary mode so it's not an issue.
Adam Pierce
+9  A: 

Its almost possible with an istream_iterator (3 lines!)

#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    ifstream file("filename.txt");
    string fileContents;

    copy(istreambuf_iterator<char>(file),
              istreambuf_iterator<char>(),
              back_inserter(fileContents));
}

Edited - got rid of intermediate string stream, now copies straight into the string, and now using istreambuf_iterator, which ignores whitespace (thanks Martin York for your comment).

Doug T.
Put that in its own function and now you've got a one-liner. Just call that function.
Zooba
Why not use istreambuf_iterator<char>(). It does not skip white space.
Martin York
@Martin York - thanks, I was actually trying to search for that, but couldn't think of it.
Doug T.
+2  A: 

How about:

#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;

int main( void )
{
  stringstream os(stringstream::out);
  os << ifstream("filename.txt").rdbuf();
  string s(os.str());
  cout << s << endl;
}
njsf
A: 
Shadow2531
+12  A: 

We can do it but it's a long line :

#include<fstream>
#include<iostream>
#include<iterator>
#include<string>

using namespace std;

int main()
{
    // The one-liner
    string fileContents(istreambuf_iterator<char>(ifstream("filename.txt")), istreambuf_iterator<char>());

    // Check result
    cout << fileContents;
}

Edited : use "istreambuf_iterator" instead of "istream_iterator"

Rexxar
You just used the ',' operator, its essentially just like putting a bunch of statements with a semicolon between them on one line. But even with that, it beats mine below by a line, nice.
Doug T.
If you use an istreambuf_iterator (see Martin York's comment on my post) you can forget the noskipws.
Doug T.
Another comment, ifstream is a temporary, can you ensure it won't close while we still have istream_iterators to it? Would the evaluation of this be compiler specific.
Doug T.
@Doug T : I don't use the ',' operator. Read the code again. I only use the string constructor with two parameters.
Rexxar
@Doug T : Temporary Objects are destroyed after the evaluation of the full-expression that is just before the semi-colon.
Rexxar
Yes on second thought, you are correct, I'll vote you up ;)
Doug T.
Nice! Just a note: I think this will fail silently (give you an empty string) if the file doesn't exist. To deal with that you need to check the ifstream's .fail().
Yang
I'm not sure that labelling one of the many lines "one-liner" makes it so :-D
Johnsyweb
A: 
std::string temp, file; std::ifstream if(filename); while(getline(if, temp)) file += temp;

It's not a short or single-statement line, but it is one line and it's really not that bad.

DeadMG