c++ - Are there any issues reading directly into an std::string? -


i've seen in code direct read std::string contents intended interpreted string follows:

std::string note; note.resize(n); read( &note[0], n ); 

assume n of fixed size, in parsing scenario.

are there issues reading directly string? have seen lot of uses of ifstreams, seems excessive in case.

first, if it's text file made several lines, find std::string class not choice "container"; prefer std::vector<char>, or if want additional parsing , break file single lines, std::vector<std::string>.

i'd pay attention encoding used file: utf-8? other char-based encoding?

for example, if file utf-16, reading raw sequence of bytes std::string misleading (and bug prone).

moreover, it's important pay attention size of file. if have gigantic text file (e.g. 5gb) , building 32-bit windows application, code won't work (as 32-bit processes on windows limited 2gb default). in such cases, reading file content in smaller chunks (or using memory-mapped file techniques smaller "views" on file) may better alternative.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -