c++ - What is the preferred way to read from a file into a char array? -
apology for the original question; I am relatively new to C ++.
I have looked around and have seen many different suggestions how to read from a file to four arrays, for example, makes a four array of size 10000, but it is sub-symmetrical (or Not wasted space, or adequate space).
What is the simple and most used method from a file to a string, or a string-like sequence? This is a normal operation; There should be a standard way to do this. Is there no one liner for this? I usually use it (like when we do not not a loop Read thousands of files in!): If you want vector, then use it instead: However, if you want to populate existing Hope that helps.
std :: ifstream file ("data.txt"); Std :: string data {std :: istreambuf_iterator & lt; Char & gt; {File}, std :: istreambuf_iterator & lt; Char & gt; {}}; No need to use
std :: copy (like the other answer, now deleted!).
std :: vector & lt; Char & gt; Data {std :: istreambuf_iterator & lt; Char & gt; {File}, std :: istreambuf_iterator & lt; Char & gt; {}};
std :: vector (or
std :: string ), then
Insert method (both types have the
insert method of the same signature!):
data.insert (data.end), / / Insert end std :: istreambuf_iterator & lt; char & gt; {file}, // source start std :: istreambuf_iterator & lt; char & gt; {}); // source end
Comments
Post a Comment