Read whole ASCII file into C++ std::string [duplicate]
Asked Answered
L

9

728

I need to read a whole file into memory and place it in a C++ std::string.

If I were to read it into a char[], the answer would be very simple:

std::ifstream t;
int length;
t.open("file.txt");      // open input file
t.seekg(0, std::ios::end);    // go to the end
length = t.tellg();           // report location (this is the length)
t.seekg(0, std::ios::beg);    // go back to the beginning
buffer = new char[length];    // allocate memory for a buffer of appropriate dimension
t.read(buffer, length);       // read the whole file into the buffer
t.close();                    // close file handle

// ... Do stuff with buffer here ...

Now, I want to do the exact same thing, but using a std::string instead of a char[]. I want to avoid loops, i.e. I don't want to:

std::ifstream t;
t.open("file.txt");
std::string buffer;
std::string line;
while(t){
std::getline(t, line);
// ... Append line to buffer and go on
}
t.close()

Any ideas?

Lello answered 8/4, 2010 at 17:19 Comment(9)
There will always be a loop involved, but it can be implicit as part of the standard library. Is that acceptable? Why are you trying to avoid loops?Hospitalet
I believe that the poster knew that reading bytes involved looping. He just wanted an easy, perl-style gulp equivalent. That involved writing little code.Pellet
This code is buggy, in the event that the std::string doesn't use a continuous buffer for its string data (which is allowed): https://mcmap.net/q/16244/-writing-directly-to-std-string-internal-buffersHartzel
@ChrisDesjardins: (1) Your link is outdated (C++11 made it contiguous) and (2) even if it wasn't, std::getline(istream&, std::string&) would still do the right thing.Burck
Side note for anyone looking at this code: The code presented as an example for reading into char[] does not null-terminate the array (read does not do this automatically), which may not be what you expect.Duumvirate
"the answer would be very simple". Understandable yes, simple no ;-)Pinnatipartite
Casting the streampos returned by tellg() into an int is not guaranteed to return the length of the file. If you subtract the streampos at the start of the file from that at the end of the file, you will get a streamoff which is guaranteed to be of an integral type and represent an offset in the file, at least in C++11. See cplusplus.com/reference/ios/streamoff and the comment in https://mcmap.net/q/57275/-what-are-the-differences-between-streampos-and-pos_type-streamoff-and-off_type. See https://mcmap.net/q/57276/-using-c-filestreams-fstream-how-can-you-determine-the-size-of-a-file for a safe version.Begotten
Why is this question closed for answers as it is super outdated and needs new answers? Plus it has more than 650 upvotes.. beyond meJerid
@AdanVivero It's closed as a duplicate. New answers should be posted on the linked question.Acquaintance
N
615

Update: Turns out that this method, while following STL idioms well, is actually surprisingly inefficient! Don't do this with large files. (See: http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html)

You can make a streambuf iterator out of the file and initialize the string with it:

#include <string>
#include <fstream>
#include <streambuf>

std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)),
                 std::istreambuf_iterator<char>());

Not sure where you're getting the t.open("file.txt", "r") syntax from. As far as I know that's not a method that std::ifstream has. It looks like you've confused it with C's fopen.

Edit: Also note the extra parentheses around the first argument to the string constructor. These are essential. They prevent the problem known as the "most vexing parse", which in this case won't actually give you a compile error like it usually does, but will give you interesting (read: wrong) results.

Following KeithB's point in the comments, here's a way to do it that allocates all the memory up front (rather than relying on the string class's automatic reallocation):

#include <string>
#include <fstream>
#include <streambuf>

std::ifstream t("file.txt");
std::string str;

t.seekg(0, std::ios::end);   
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);

str.assign((std::istreambuf_iterator<char>(t)),
            std::istreambuf_iterator<char>());
Negativism answered 8/4, 2010 at 17:23 Comment(25)
open is definitely a method of ifstream, however the 2nd parameter is wrong. cplusplus.com/reference/iostream/ifstream/openBishop
Right. I was saying that ifstream doesn't have a method with the signature open(const char*, const char*)Negativism
This is just making the explicit loop implicit. Since the iterator is a forward iterator, it will be read one character at a time. Also, since there is no way for the string constructor to know the final length, it will probably lead to several allocations and copies of the data.Benavidez
Yep, I am starting off with C++ and I'm still quite illiterate. Thanks for the answer, though, it is exactly what I needed. +1.Lello
no second parameter is required - ifstreams are input streamsAfterward
@Benavidez If efficiency is important, you could find the file length the same was as in the char* example and call std::string::reserve to preallocate the necessary space.Negativism
@KeithB: Of course, the read() method undoubtedly has lots of looping going on. The question is not whether it loops but where and how explicitly.Persaud
In the str.assign() approach the first argument's parentheses are unnecessary, because it can't parse as a declaration.Elwira
Note that the file may be longer than the string. If your OS uses <CR><LF> (two chars) as a line separator, the string will use '\n' (one char). Text streams do conversions to and from '\n' to the underlying representation.Hospitalet
@Adrian '\n' is merely a portable way of specifying newline in C code. Down below the compiler will still translate '\n' to what's appropriate for a newline for the compiler's operating system.Elwira
No sure why people are voting this up, here is a quick question, say I have a 1MB file, how many times will the "end" passed to the std::string constructor or assign method be invoked? People think these kind of solutions are elegant when in fact they are excellent examples of HOW NOT TO DO IT.Bouchier
@Matthieu N. You're going to have to explain that a little more, it looks like once to me.Classmate
@MaxEhrlich he probably means dereferencing. for a 1MB file this would require about 1M compares, which doesn't seem really efficient.Cryogenics
Benchmarked: both Tyler's solutions take about 21 seconds on a 267 MB file. Jerry's first takes 1.2 seconds and his second 0.5 (+/- 0.1), so clearly there's something inefficient about Tyler's code.Adsorb
@Adsorb You're right. About a year after I wrote this post, somebody did some benchmarking of various approaches to this problem and found that reserve+assign unfortunately does not seem to work the way that you would hope it did. And it turns out that in general iterators produce a surprisng amount of overhead. Disappointing. Edited this into the post.Negativism
The insanecoding blog post is benchmarking solutions to a slightly different problem: it is reading the file as binary not text, so there's no translation of line endings. As a side effect, reading as binary makes ftell a reliable way to get the file length (assuming a long can represent the file length, which is not guaranteed). For determining the length, ftell is not reliable on a text stream. If you're reading a file from tape (e.g., a backup), the extra seeking may be a waste of time. Many of the blog post implementations don't use RAII and can therefore leak if there's an error.Hospitalet
Luke, use std::ios::ate std::ifstream t("file.txt", std::ios::in | std::ios::binary | std::ios::ate); str.reserve(t.tellg());Decane
While this answer is highly ranked, with the updates and edits stating that the method is slow now it's a mess. Do you have a method that is using stl facilities that is also fast? If so, clean all the mess and just write it in a concise way.Spender
yep,this answer is quite messy. In particular: does the update ("dont do this with large files") refer to the first code? what exactly is the inefficiency? does the second code fix it?Vehicular
With C++17 you can shorten the std::string initialization line quite nicely (and similarly for the str.assign method): std::string str{std::istreambuf_iterator{in}, {}};. This uses C++11 brace initialization syntax and C++17 deduction guides (to omit the <char>).Histogram
Well, I used this solution and I found that it will stop at the first null char when I read a file that contains null char paddings. That's really annoying. It should be better just to use t.read(buffer_.c_str(), size)".Tobe
@Tyler McHenry You said "reserve+assign unfortunately does not seem to work the way that you would hope it did". Does this mean that the reserved memory is discarded and the string is reallocated at its default size when assign is called with iterators? If so, why not use std::copy, from the streambuf_iterators, to a std::back_inserter on the string? std::copy would just use push_back on the string, via the std::back_inserter, so there's no way reserved string memory would shrink this way.Chief
It would remove the string reallocations throughout the read, which I think would be the majority of the performance slowdown with this method. Iterator overhead I think would just be an extra function call or two per character, which I hope would be inlined with sufficiently aggressive optimization. I may try profiling this...Chief
He is getting t.open("file.txt", "r") from python.Queasy
This gives potential -Wnull-dereference warning: godbolt.org/z/KojKWT4WK - not really sure why, the error is hard to read.Muscolo
C
1110

There are a couple of possibilities. One I like uses a stringstream as a go-between:

std::ifstream t("file.txt");
std::stringstream buffer;
buffer << t.rdbuf();

Now the contents of "file.txt" are available in a string as buffer.str().

Another possibility (though I certainly don't like it as well) is much more like your original:

std::ifstream t("file.txt");
t.seekg(0, std::ios::end);
size_t size = t.tellg();
std::string buffer(size, ' ');
t.seekg(0);
t.read(&buffer[0], size); 

Officially, this isn't required to work under the C++98 or 03 standard (string isn't required to store data contiguously) but in fact it works with all known implementations, and C++11 and later do require contiguous storage, so it's guaranteed to work with them.

As to why I don't like the latter as well: first, because it's longer and harder to read. Second, because it requires that you initialize the contents of the string with data you don't care about, then immediately write over that data (yes, the time to initialize is usually trivial compared to the reading, so it probably doesn't matter, but to me it still feels kind of wrong). Third, in a text file, position X in the file doesn't necessarily mean you'll have read X characters to reach that point -- it's not required to take into account things like line-end translations. On real systems that do such translations (e.g., Windows) the translated form is shorter than what's in the file (i.e., "\r\n" in the file becomes "\n" in the translated string) so all you've done is reserved a little extra space you never use. Again, doesn't really cause a major problem but feels a little wrong anyway.

Corse answered 8/4, 2010 at 17:53 Comment(37)
The three-liner works like a charm!Fylfot
This should've been marked as the answer.Pellet
Important note for some, at least on my implementation, the three-liner works at least as good as the C fopen alternative for files under 50KB. Past that, it seems to lose performance fast. In which case, just use the second solution.Supermundane
make sure to #include <sstream>Petr
Should also check to see if the file has opened, e.g., if (!t) std::cerr << "Error opening file." << std::endl;. Of course, don't forget to close the file as well when you are done.Crew
Most of the time, you're fine not testing whether the file has opened (the other operations will simply fail). As a rule, you should avoid printing out error messages on the spot, unless you're sure that fits with the rest of the program -- if you must do something, throwing an exception is usually preferable. You should almost never explicitly close a file either -- the destructor will do that automatically.Corse
According to my testing (GCC 4.7), the buffer contains the same number of characters as the file size no matter which line endings are used. I'm guessing read(buf, size) turns off these conversions — anyone know?Adsorb
Where is the data stored in example 2?Divider
@DissidentRage: Into buffer.Corse
Wouldn't constructing an empty string and then calling reserve(size) on it be more efficient?Transatlantic
If anyone is still interested, the answer to the question of dhardy can be found in the ifstream doc: " This function simply copies a block of data, without checking its contents nor appending a null character at the end."Menhaden
If you want to get the file as a std::string see #116538 for a one liner solution.Brezin
fwiw, on OSX 10.10, I needed to #include <fstream> instead of <sstream>Suck
@anthropomorphic You should not use reserve(), because the size() information is not correctly maintained and the string is in a broken state!Confide
Can you get the number of chars read in the t.read() call and use that to set the string length.Darla
@Jasen: Not really--you want to set the length before you do the read, so you'll have enough space to read into. By the time you call read, it's too late to set the size.Corse
@Confide The suggestion was wrong, but your description seems off. One cannot reserve then read into the reserved space because no elements exist in the new space. Operations are only valid on elements between begin and begin + size - 1. The reserve only increases capacity, beyond size. Only the space exists there; elements do not. To create elements, one must use resize, emplace_back, etc. That's why, if using the 2nd method here, the container must first have its entire size declared and all elements default constructed... just so that they can immediately be overwritten.Assembly
OP asked for code to read an ASCII file into a string. Will this read any file, or is there something ASCII-specific lurking under the hood?Ropeway
@einpoklum: No. It probably doesn't make much sense to read into a string unless your data is actually a string, but that's not really a limitation, just good sense.Corse
After puzzling over this for a few minutes (compiler errors -- Windows 10, VS2015), I found I need to include BOTH #include <sstream> and #include <fstream>. Best of luck!Pops
@Darla It is possible to get the total read characters (not bytes/chars!) by using the std::basic_istream::gcount function. I believe one should strip of the unused bytes by adding a buffer.resize(t.gcount());.Dickerson
What are the downsides of combining this into a single line: (std::stringstream() << std::ifstream("file.txt").rdbuf()).str() ?Grano
@AlecJacobson: The primary downside is that it won't compile. If you really want it as a single expression, you can do it by adding a cast: static_cast<std::stringstream &>(std::stringstream() << std::ifstream("file.txt").rdbuf()).str();. IMO, it's more readable as separate statements though.Corse
Seems to compile with clang on Mac. Is that just an accident? Why should it not have compiled?Grano
@AlecJacobson: Because the operator<< that takes a pointer to a streambuf returns an ostream & --i.e., a pointer to the base object. That's usually fine (for things like chaining), but to use .str(), you need a stringstream instead. The cast takes the reference to base being returned and casts it back to be a reference to the actual type.Corse
Your second solution is less elegant, but it is 3 times faster !Lewislewisite
I'm late here too but thought I would comment for those less initiated like myself. The second solution is quite a bit faster and a little less elegant as Pico12 points out. The thing that wasn't absolutely clear to me (and maybe it's just semantics), was what problems the differences in end line characters might cause. I'm using Codeblocks on win10 with Mingw-w64 GCC. It's not just that you've stored extra space. It's that for every '\n' character, the infile character count is 1 less than what ends up stored in std::string buffer. This can cause string nav to be problematic.Phipps
If you want to display the buffer string --> std::cout << buffer.str() << std::endl;Eo
@JerryCoffin , what are the exceptions you'd have to check against for this?Fanlight
brilliant!! i've used in:string read_file(string file_name) { std::stringstream buffer; buffer << ifstream(file_name).rdbuf(); return buffer.str(); }Cardew
Three-liner is short, but it's confusing. rdbuf() returns filebuf*. How does putting pointer to rdbuf makes stringstream to read file content? I would prefer more verbose, but more clear code than this magic.Desinence
Error checking is missing as mentioned by @RaffiKhatchadourian. Whenever you work with files I strongly recommend doing some error handling.Fractious
3 liner is WRONG, there is no guarantee that entire file fits into buffer, read reddit.com/r/Cplusplus/comments/6cpekc/what_does_rdbuf_do/…Picul
@NoSenseEtAl: The Reddit comment is irrelevant. See §[ostream.inserters]/7: "Gets characters from sb and inserts them in *this. Characters are read from sb and inserted until any of the following occurs: (8.1) — end-of-file occurs on the input sequence; (8.2) — inserting in the output sequence fails (in which case the character to be inserted is not extracted); (8.3) — an exception occurs while getting a character from sb." So, it attempts to copy the remainder of the file controlled by the streambuffer, regardless of how much/little may currently be contained in the streambuffer.Corse
Strangely I don't obtain the same results with the two variants (I tried with both text or binary open mode) when I read a file from WSL with a shared NFS file system mounted in windows then in linux (via drvfs). I don't know why, but the second variant seems OK while the first one generates some random json parsing issues.Tort
the size of the 3 lines solution is wrong. buffer.str().size() is wrong!Gelt
Doesn't work if the file is too long/large.Viand
N
615

Update: Turns out that this method, while following STL idioms well, is actually surprisingly inefficient! Don't do this with large files. (See: http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html)

You can make a streambuf iterator out of the file and initialize the string with it:

#include <string>
#include <fstream>
#include <streambuf>

std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)),
                 std::istreambuf_iterator<char>());

Not sure where you're getting the t.open("file.txt", "r") syntax from. As far as I know that's not a method that std::ifstream has. It looks like you've confused it with C's fopen.

Edit: Also note the extra parentheses around the first argument to the string constructor. These are essential. They prevent the problem known as the "most vexing parse", which in this case won't actually give you a compile error like it usually does, but will give you interesting (read: wrong) results.

Following KeithB's point in the comments, here's a way to do it that allocates all the memory up front (rather than relying on the string class's automatic reallocation):

#include <string>
#include <fstream>
#include <streambuf>

std::ifstream t("file.txt");
std::string str;

t.seekg(0, std::ios::end);   
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);

str.assign((std::istreambuf_iterator<char>(t)),
            std::istreambuf_iterator<char>());
Negativism answered 8/4, 2010 at 17:23 Comment(25)
open is definitely a method of ifstream, however the 2nd parameter is wrong. cplusplus.com/reference/iostream/ifstream/openBishop
Right. I was saying that ifstream doesn't have a method with the signature open(const char*, const char*)Negativism
This is just making the explicit loop implicit. Since the iterator is a forward iterator, it will be read one character at a time. Also, since there is no way for the string constructor to know the final length, it will probably lead to several allocations and copies of the data.Benavidez
Yep, I am starting off with C++ and I'm still quite illiterate. Thanks for the answer, though, it is exactly what I needed. +1.Lello
no second parameter is required - ifstreams are input streamsAfterward
@Benavidez If efficiency is important, you could find the file length the same was as in the char* example and call std::string::reserve to preallocate the necessary space.Negativism
@KeithB: Of course, the read() method undoubtedly has lots of looping going on. The question is not whether it loops but where and how explicitly.Persaud
In the str.assign() approach the first argument's parentheses are unnecessary, because it can't parse as a declaration.Elwira
Note that the file may be longer than the string. If your OS uses <CR><LF> (two chars) as a line separator, the string will use '\n' (one char). Text streams do conversions to and from '\n' to the underlying representation.Hospitalet
@Adrian '\n' is merely a portable way of specifying newline in C code. Down below the compiler will still translate '\n' to what's appropriate for a newline for the compiler's operating system.Elwira
No sure why people are voting this up, here is a quick question, say I have a 1MB file, how many times will the "end" passed to the std::string constructor or assign method be invoked? People think these kind of solutions are elegant when in fact they are excellent examples of HOW NOT TO DO IT.Bouchier
@Matthieu N. You're going to have to explain that a little more, it looks like once to me.Classmate
@MaxEhrlich he probably means dereferencing. for a 1MB file this would require about 1M compares, which doesn't seem really efficient.Cryogenics
Benchmarked: both Tyler's solutions take about 21 seconds on a 267 MB file. Jerry's first takes 1.2 seconds and his second 0.5 (+/- 0.1), so clearly there's something inefficient about Tyler's code.Adsorb
@Adsorb You're right. About a year after I wrote this post, somebody did some benchmarking of various approaches to this problem and found that reserve+assign unfortunately does not seem to work the way that you would hope it did. And it turns out that in general iterators produce a surprisng amount of overhead. Disappointing. Edited this into the post.Negativism
The insanecoding blog post is benchmarking solutions to a slightly different problem: it is reading the file as binary not text, so there's no translation of line endings. As a side effect, reading as binary makes ftell a reliable way to get the file length (assuming a long can represent the file length, which is not guaranteed). For determining the length, ftell is not reliable on a text stream. If you're reading a file from tape (e.g., a backup), the extra seeking may be a waste of time. Many of the blog post implementations don't use RAII and can therefore leak if there's an error.Hospitalet
Luke, use std::ios::ate std::ifstream t("file.txt", std::ios::in | std::ios::binary | std::ios::ate); str.reserve(t.tellg());Decane
While this answer is highly ranked, with the updates and edits stating that the method is slow now it's a mess. Do you have a method that is using stl facilities that is also fast? If so, clean all the mess and just write it in a concise way.Spender
yep,this answer is quite messy. In particular: does the update ("dont do this with large files") refer to the first code? what exactly is the inefficiency? does the second code fix it?Vehicular
With C++17 you can shorten the std::string initialization line quite nicely (and similarly for the str.assign method): std::string str{std::istreambuf_iterator{in}, {}};. This uses C++11 brace initialization syntax and C++17 deduction guides (to omit the <char>).Histogram
Well, I used this solution and I found that it will stop at the first null char when I read a file that contains null char paddings. That's really annoying. It should be better just to use t.read(buffer_.c_str(), size)".Tobe
@Tyler McHenry You said "reserve+assign unfortunately does not seem to work the way that you would hope it did". Does this mean that the reserved memory is discarded and the string is reallocated at its default size when assign is called with iterators? If so, why not use std::copy, from the streambuf_iterators, to a std::back_inserter on the string? std::copy would just use push_back on the string, via the std::back_inserter, so there's no way reserved string memory would shrink this way.Chief
It would remove the string reallocations throughout the read, which I think would be the majority of the performance slowdown with this method. Iterator overhead I think would just be an extra function call or two per character, which I hope would be inlined with sufficiently aggressive optimization. I may try profiling this...Chief
He is getting t.open("file.txt", "r") from python.Queasy
This gives potential -Wnull-dereference warning: godbolt.org/z/KojKWT4WK - not really sure why, the error is hard to read.Muscolo
I
97

I think best way is to use string stream. simple and quick !!!

#include <fstream>
#include <iostream>
#include <sstream> //std::stringstream
int main() {
    std::ifstream inFile;
    inFile.open("inFileName"); //open the input file

    std::stringstream strStream;
    strStream << inFile.rdbuf(); //read the file
    std::string str = strStream.str(); //str holds the content of the file

    std::cout << str << "\n"; //you can do anything with the string!!!
}
Ivories answered 12/11, 2013 at 6:8 Comment(8)
Simple and quick, right! insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.htmlQuint
Remember to close the stream afterwards...Lagoon
@YngveSneenLindal Or let the destructor do it automatically - take advantage of C++!Claymore
But in Cfree 5, string is not identified as a name type. In that case, will 'char' type work?Musso
@YngveSneenLindal are you sure you need to close the stream afterwards? Apparently the fstream's memory allocation should be deallocated once fstream is destroyed (out of the scope)? Although it can be good to use .close() for error checking?Sokotra
you can close stream, if you need. but it is not compulsory. see this #748514Ivories
You're right @DanNissenbaum. Agree!Lagoon
Why post this when it was already in the answer by Jerry Coffin from years before?Suasion
L
39

You may not find this in any book or site, but I found out that it works pretty well:

#include <fstream>
// ...
std::string file_content;
std::getline(std::ifstream("filename.txt"), file_content, '\0');
Listless answered 10/11, 2015 at 16:48 Comment(6)
Casting eof to (char) is a bit dodgy, suggesting some kind of relevance and universality which is illusory. For some possible values of eof() and signed char, it will give implementation-defined results. Directly using e.g. char(0) / '\0' would be more robust and honestly indicative of what's happening.Eye
@TonyD. Good point about converting eof() to a char. I suppose for old-school ascii character sets, passing any negative value (msb set to 1) would work. But passing \0 (or a negative value) won't work for wide or multi-byte input files.Fabian
This will only work, as long as there are no "eof" (e.g. 0x00, 0xff, ...) characters in your file. If there are, you will only read part of the file.Kalina
@OlafDietsche There shouldn't be 0x00 in an ASCII file (or I wouldn't call it ASCII file). 0x00 appears to me like a good option to force the getline() to read the whole file. And, I must admit that this code is as short as easy to read although the higher voted solutions look much more impressive and sophisticated.Orcus
@Scheff After revisiting this answer, I don't know, how I reached to that conclusion and comment. Maybe I thought, that (char) ifs.eof() has some meaning. eof() returns false at this point, and the call is equivalent to std::getline(ifs, s, 0);. So it reads until the first 0 byte, or the end of file, if there's no 0 byte.Kalina
Could you please explain how '\0' is a valid delimiter ?Wyrick
U
7

Try one of these two methods:

string get_file_string(){
    std::ifstream ifs("path_to_file");
    return string((std::istreambuf_iterator<char>(ifs)),
                  (std::istreambuf_iterator<char>()));
}

string get_file_string2(){
    ifstream inFile;
    inFile.open("path_to_file");//open the input file

    stringstream strStream;
    strStream << inFile.rdbuf();//read the file
    return strStream.str();//str holds the content of the file
}
Unteach answered 5/2, 2015 at 12:50 Comment(0)
S
2

I figured out another way that works with most istreams, including std::cin!

std::string readFile()
{
    stringstream str;
    ifstream stream("Hello_World.txt");
    if(stream.is_open())
    {
        while(stream.peek() != EOF)
        {
            str << (char) stream.get();
        }
        stream.close();
        return str.str();
    }
}
Socialism answered 20/8, 2014 at 5:23 Comment(0)
U
1

If you happen to use glibmm you can try Glib::file_get_contents.

#include <iostream>
#include <glibmm.h>

int main() {
    auto filename = "my-file.txt";
    try {
        std::string contents = Glib::file_get_contents(filename);
        std::cout << "File data:\n" << contents << std::endl;
    catch (const Glib::FileError& e) {
        std::cout << "Oops, an error occurred:\n" << e.what() << std::endl;
    }

    return 0;
}
Unlike answered 7/8, 2016 at 14:55 Comment(1)
Imho: Although this works, providing a "glib" solution, which is the non-platform-independent equivalent of pandora's chest, might confuse enormously, even more, if there's a simple CPP-standard solution to it.Ouse
O
0

I could do it like this:

void readfile(const std::string &filepath,std::string &buffer){
    std::ifstream fin(filepath.c_str());
    getline(fin, buffer, char(-1));
    fin.close();
}

If this is something to be frowned upon, please let me know why

Odoacer answered 22/10, 2012 at 12:55 Comment(2)
char(-1) is probably not a portable way to denote EOF. Also, getline() implementations are not required to support the "invalid" EOF pseudo-character as a delimiter character, I think.Belda
@Belda it indeed isn't, in modern C++ it is better to use std::char_traits<char>::eof(). If someone is still using an ancient compiler... <cstdio> contains EOF macro.Bromo
B
-6

I don't think you can do this without an explicit or implicit loop, without reading into a char array (or some other container) first and ten constructing the string. If you don't need the other capabilities of a string, it could be done with vector<char> the same way you are currently using a char *.

Benavidez answered 8/4, 2010 at 17:30 Comment(2)
-1 Not true... See abovePellet
Well, to be fair, all of the answers above do include a loop in some way, be it as a boilerplate or behind the scenes...Anchylose

© 2022 - 2024 — McMap. All rights reserved.