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
208
votes
8 answers

What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

When would I use std::istringstream, std::ostringstream and std::stringstream and why shouldn't I just use std::stringstream in every scenario (are there any runtime performance issues?). Lastly, is there anything bad about this (instead of using a…
Oliver Baur
  • 2,295
  • 2
  • 16
  • 7
130
votes
2 answers

C++ compile error: has initializer but incomplete type

I am coding in Eclipse and have something like the following: #include #include void read_file(){ char buffer[1025]; std::istringstream iss(buffer); } However, when I try to build, I get the following error: variable…
ElectronAnt
  • 2,115
  • 7
  • 22
  • 39
21
votes
2 answers

C++ - repeatedly using istringstream

I have a code for reading files with float numbers on line stored like this: "3.34|2.3409|1.0001|...|1.1|". I would like to read them using istringstream, but it doesn't work as I would expect: string row; string strNum; istringstream…
Naomak
  • 409
  • 1
  • 4
  • 9
20
votes
2 answers

Splitting a string into integers using istringstream in C++

I'm trying to use istringstream to split a simple string into a series of integers: #include #include #include #include using namespace std; int main(){ string s = "1 2 3"; istringstream iss(s); …
Znorg
  • 681
  • 1
  • 7
  • 19
19
votes
1 answer

C++, function that can take either ifstream or istringstream

I have a function do_something that reads unsigned characters from a stream. The stream can be created from a file given the file name. Or it can be created from the given string by considering it as data. I would like to reuse the function in both…
klk206
  • 454
  • 4
  • 8
19
votes
1 answer

g++ and clang++ different behaviour with stream input and unsigned integer

I came across a difference in behavior, between gcc (4.9.2) and clang (3.5.0), which surprised me. When I try to feed an unsigned int from an std::istringstream initialized with a negative value ("-15", in the example) I get an error (with fail()…
max66
  • 65,235
  • 10
  • 71
  • 111
17
votes
5 answers

non-copying istringstream

So istringstream copies the contents of a string when initialised, e.g string moo("one two three four"); istringstream iss(moo.c_str()); I was wondering if there's a way to make std::istringstream use the given c_str as its buffer without copying…
kamziro
  • 7,882
  • 9
  • 55
  • 78
13
votes
6 answers

How to assign istringstream and ifstream to an istream variable?

I want to have a variable of type istream which can hold either the contents of a file or a string. The idea is that if no file was specified, the variable of type istream would be assigned with a string. std::ifstream…
oddRaven
  • 672
  • 1
  • 7
  • 20
6
votes
3 answers

Can operator>> read an int hex AND decimal?

Can I persuade operator>> in C++ to read both a hex value AND and a decimal value? The following program demonstrates how reading hex goes wrong. I'd like the same istringstream to be able to read both hex and decimal. #include #include…
mes5k
  • 855
  • 10
  • 15
6
votes
3 answers

The role of std::ws (whitespace) when reading data

Data saved in my file is (white spaces added at both beginning and end on purpose for this test): 1 2 3 Loading the data using the code below with or without "std::ws" does not cause any difference. So I am confused by the role of "std::ws"…
H. Wang
  • 61
  • 1
  • 1
  • 2
5
votes
2 answers

Boost property_tree error: conversion of data to type "j" failed when getting an element in a .ini file

I have a .ini file contains below data [SYSTEM] num_of_vps = 1 And I have this code to read an element in .ini file. (uint defined as typedef unsigned int uint) boost::property_tree::ptree pt;…
5
votes
1 answer

Reading int with specific field width from stringstream

I'm trying to read bytes in hex notation from a string. The bytes may or may unfortunately not be separated by whitespace, e.g. " 00 ffab c " is a valid example and should result in 4 bytes read, 0x00, 0xff, 0xab and 0x0c. The problem is to skip…
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
5
votes
4 answers

how to convert ifstream to istringstream

I have a function that reads through an istringstream and does some operations on it so ... I am trying to read in a file using fstream and convert the fstream object to an istringstream in order to pass it to my function. I don't know how to do…
user5932842
  • 67
  • 1
  • 7
5
votes
2 answers

do...while() repeating the last string twice

The following code splits the provided string/line down to the characters. Why does the loop repeats that last string twice? How to fix it? #include #include #include #include using namespace std; int…
Bot666
  • 61
  • 6
5
votes
0 answers

std::getline deal with \n, \r and \r\n

I'm reading data in from a text file and do so line by line using std::getline. This by default reads up to a newline character \n. My code relies on this. However, it turns out I have to deal with data that may be generated in different…
user3353819
  • 911
  • 2
  • 8
  • 21
1
2 3
14 15