Questions tagged [istringstream]

Anything related to C++ `istringstream` standard library class. This class represents an "input string stream", i.e. an input stream that allows a string to be used as the source of information attached to the stream.

Anything related to C++ istringstream standard library class. This class represents an input string stream, i.e. an input stream that allows a string to be used as the source of information attached to the stream.

istringstream is a specialization of the basic_istringstream template class and it is defined in C++ <sstream> standard library header.

See CPPreference.com on basic_istringstream template class.

216 questions
3
votes
0 answers

Why does string_stream.str() = a_string; compile?

I can create an std::istringstream like so std::istringstream string_stream(inp_string); I can then change the contents of the stream: string_stream.str(std::move(inp_string_2)); However, I might accidentally try something like…
3
votes
0 answers

Reading string by string in a file's lines using a for loop in C++

I need help with my C++ code, which reads data from an ASCII file and stores them into an std::vector. The data are stored consecutively (as in the example below). For some reasons I want to substitute the while(iss>>token) with a for loop,…
fslack
  • 189
  • 9
3
votes
2 answers

Extraction operator into non-numeric character

Given an istringstream, is it possible to "extract" its contents into a character only if the character to be extracted is non-numeric (i.e. not 0-9)? For example, this string foo = "+ 2 3"; istringstream iss(foo); char c; iss >> skipws >> c; …
Anakhand
  • 2,838
  • 1
  • 22
  • 50
3
votes
0 answers

Redirect C stdin to read from C++ stringstream

I have some code that was provided to me written in C that I'm using in a C++ project. I need to add some functionality, along with unit tests. Unfortunately it's hard coded to use getchar and other C IO functions in a way that isn't very easy to…
anderspitman
  • 9,230
  • 10
  • 40
  • 61
3
votes
3 answers

istringstream ignores first letter

I am trying to access different words in a string using std::istringstream and I am also doing so with multiple test cases. int t; cin>>t; while(t--) { string arr; cin.ignore(); getline(cin,arr); istringstream iss(arr); …
amora
  • 135
  • 2
  • 8
3
votes
2 answers

Reading a double from an istream in Hex

Given double foo I can assign it from a hex format string using sscanf like this: sscanf("0XD", "%lg", &foo) But I cannot seem to get an istringstream to behave the same way. All of these just write 0 to foo: istringstream("0XD") >>…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
2 answers

Cast string object to istringstream

#include #include #include #include #include void reverse_words(const std::string &file) { std::ifstream inFile { file }; std::vector lines; if(static_cast(inFile))…
3
votes
1 answer

std::getline skipping input from std::cin after last occurrence of delimiter, but not with input from std::istringstream

I need to read some input that is delimited by a whitespace, the main construction I used for this is: while(std::getline(std::cin, s, ' ')){ std::cout << s << std::endl; } For the input: "this is some text" The output for S will be: "this",…
3
votes
1 answer

Extracting bool from istream in a templated function

I'm converting my fields class read functions into one template function. I have field classes for int, unsigned int, long, and unsigned long. These all use the same method for extracting a value from an istringstream (only the types change): …
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
3
votes
1 answer

Convert string to __uint128_t using stringstreams

I'm trying to extract different types of data from a string. void readHeader(char buf[BUFFSIZE]) { std::istringstream hdr(buf); __uint128_t id_client; hdr >> id_client; // doesn't compile } I'm getting this…
Amina
  • 404
  • 4
  • 14
3
votes
2 answers

How to save and restore an std::istringstream's buffer?

I am using a istringstream to read a string word by word. However, when my condition fails I need to be able to revert the istringstream to before the previous word was read. My example code works, but I want to know if there is a more direct way…
user870130
  • 565
  • 1
  • 8
  • 16
3
votes
1 answer

Passing temporary istringstream object to istream_iterator

I have a question about the following code that tokenizes a string (separates the tokens by space). #include #include #include #include #include using namespace std; int main() { string s="And…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
3
votes
1 answer

C++ Adapting sscanf code to istringstream with error checking

So I've got some code that splits a string into some other variables with sscanf that looks like this. if(sscanf(input_line.c_str(), "%s %s %lf %lf %lf", &string1, &string2, &point1, &point2, &point3) != 5) { //does stuff throw; } I use…
Max Rahm
  • 684
  • 11
  • 25
3
votes
1 answer

How to convert istringstream object to string and char array?

I have this object: istringstream ob_stream; I need to convert this object to string and char array for work. How to do?
user1779502
  • 573
  • 2
  • 8
  • 16
2
votes
1 answer

Obtaining start position of istringstream token

Is there a way to find the start position of tokens extracted by istringstream::operator >>? For example, my current failed attempt at checking tellg() (run online): string test = " first \" in \\\"quotes \" last"; istringstream…
Jason C
  • 38,729
  • 14
  • 126
  • 182
1 2
3
14 15