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!):

  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!).

If you want vector, then use it instead:

  std :: vector & lt; Char & gt; Data {std :: istreambuf_iterator & lt; Char & gt; {File}, std :: istreambuf_iterator & lt; Char & gt; {}};   

However, if you want to populate existing 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   

Hope that helps.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -