I'm pretty familiar with most of C++ but one area I've avoided has been IO streams, mainly because I've been using it on embedded systems where they're not appropriate. Recently I've had to become familiar with them, however, and I'm struggling to figure out something that I feel should be simple.
What I'm looking for a relatively efficient way to read a fixed number of characters from a C++ stream into a std::string
. I could easily read into a temporary char
array with the read()
method and convert that into a std::string
, but that's fairly ugly and involves a wasteful copy. I could also read the whole of a stream into a string with something like this:
std::string getFile(std::fstream &inFile)
{
std::stringstream buffer;
buffer << inFile.rdbuf();
return buffer.str();
}
... But unbounded reads into memory are generally a poor idea, so I'd really like to access the file one block at a time, say 4K or so. I could also read character at a time, but that just feels both uglier and less efficient than reading into a temporary char
array.
So, is there a simple way to get a std::string
directly from a stream which contains the next N characters from the stream? It may well be that there is simply no way to do this, but it seems strange to me that such a thing would be missing so I felt I must be missing something glaringly obvious.
By the way, I'm quite familiar with the C file IO APIs and I'm not looking for a solution involving them. I could knock something up with read()
and write()
, but the code I'm working with makes heavy use of streams and I think it's good practice to keep my code additions in a consistent style.
You can use read() to specify the number of characters to read: char b[3] = ""; ifstream f("prad. txt"); f. read(b, sizeof(b) - 1); // Read one less that sizeof(b) to ensure null cout << b; // terminated for use with cout.
string at() in C++std::string::at can be used to extract characters by characters from a given string. Syntax 2: const char& string::at (size_type idx) const idx : index number Both forms return the character that has the index idx (the first character has index 0).
Other ways to read a std::istreamTo read a line of input, try the getline() function. For this, you need #include <string> in addition to #include <iostream> . To read a single char: use the get() method. To read a large block of characters, either use get() with a char[] as the argument, or use read() .
std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
You are on the right track. :)
mystring.resize( 20 );
stream.read( &mystring[0], 20 );
Edit:
In C++11. this is well-defined and safe. Unlike data()
and c_str()
, std::string::operator[]
provides a mutable reference to the underlying data.
In C++03, It is safe in a practical sense, but the definition of std::string
was too weak to guarantee this safety. C++03 allowed string data to be non-contiguous, but I don't believe any available implementation ever took advantage of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With