Questions tagged [ostream]

In C++ std::ostream is the base class for output streams.

765 questions
20
votes
5 answers

Why the constructor of std::ostream is protected?

Why I can't just create an "empty" stream for my output like this std::ostream out; ? This rows is apparently illegal with both clang 3.4 and gcc 4.8.1 under linux with libstdc++, I really don't get why, I mean why I can't just create a stream out…
user2485710
  • 9,451
  • 13
  • 58
  • 102
19
votes
4 answers

C++: What is the difference between ostream and ostringstream?

What is the difference between ostream and ostringstream? When would you use one versus the other?
kmccoy
  • 2,761
  • 3
  • 25
  • 29
18
votes
4 answers

Reading getline from cin into a stringstream (C++)

So I'm trying to read input like this from the standard input (using cin): Adam English 85 Charlie Math 76 Erica History 82 Richard Science 90 My goal is to eventually store each data piece in its own cell in a data structure I…
user313
  • 681
  • 1
  • 8
  • 21
18
votes
2 answers

Setting minimum number of decimal places for std::ostream precision

Is there a way to set the "minimum" number of decimal places that a std::ostream will output? For example, say I have two unknown double variables that I want to print (values added here for the sake of illustration): double a = 0; double b =…
Phil Boltt
  • 786
  • 6
  • 14
16
votes
2 answers

Is operator<<(ostream&, obj) on two different streams thread safe?

#include #include #include using namespace std; int main() { auto runner = []() { ostringstream oss; for (int i=0; i<100000; ++i) oss << i; }; thread t1(runner), t2(runner); …
lz96
  • 2,816
  • 2
  • 28
  • 46
16
votes
1 answer

Why can't I initialize a reference to `ofstream` / `ifstream`, with an instance of `fstream`?

INTRODUCTION void read_foo (std::ifstream& out); void write_foo (std::ofstream& out); I have these two functions where one is supposed to read from a file, and the other is supposed to write to one. Everything works having the below…
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
15
votes
1 answer

Using a vector of unique pointers to an vector

For a school assignment, I am trying to use a vector of unique pointer's to Employee objects to access the Employee data, but can't figure out the syntax/compiler errors. Can anybody please tell me what I am doing wrong? Have to use a vector of…
CGutz
  • 537
  • 2
  • 7
  • 20
15
votes
5 answers

What is the difference between flush() and sync() in regard to fstream buffers?

I was reading the cplusplus.com tutorial on I/O. At the end, it says fstream buffers are synchronized with the file on disc Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These…
cjcurrie
  • 624
  • 1
  • 4
  • 15
15
votes
5 answers

How to write 'n' copies of a character to ostream like in python

In python, the following instruction: print 'a'*5 would output aaaaa. How would one write something similar in C++ in conjunction with std::ostreams in order to avoid a for construct?
Mihai Bişog
  • 998
  • 9
  • 24
14
votes
6 answers

How to detect if a ptr is still referencing a valid reference after that reference goes out of scope

I am toying around with streams for a bit and can't get my head around the following. Here we have a basic ostream ptr that is set to different output streams, whether it is cout, cerr or a file. // ostream ptr std::ostream* outstream; // set…
Montaldo
  • 863
  • 8
  • 16
13
votes
3 answers

why is std::cout convertible to void* if using g++?

Why can one cast a std::ostream to a void pointer? I am not aware of any such conversion operator in std::ostream. Code below #include int main() { void *p = std::cout; // why does this work? } I'm asking this question since I've…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
12
votes
2 answers

Why is ranges::ostream_iterator default-constructible?

This question follows a discussion in the comments here. In Eric Niebler's ranges-v3 library (which is sort-of becoming part of the standard for C++20), ranges::ostream_iterator is default-constructible - without an ostream. How come? I thought…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
12
votes
1 answer

C++ When are characters widened in output stream operator<<()?

It seems to me, that there is an inconsistency in the C++ standard, specifically in §30.7.5.2.4 of the C++17 draft (N4659), about when characters are widened in formatted output operations on output streams (operator<<()). Exactly the same…
Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
12
votes
4 answers

Order of execution in operator <<

I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below A1B2 While I can see that the output I get is BA12 I thought that the call std::cout<< b->fooA() << b->fooB() <<…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
11
votes
4 answers

How can I make an ostream reference an ofstream? (C++)

I'm trying to make a simple logger class, and I want the ability to either log to either a generic ostream (cout/cerr) or a file. The design I have in mind is to allow the constructor to either take an ostream& or a filename, and in the latter case…
user1163857
1
2
3
50 51