ostream

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

和自甴很熟 提交于 2019-11-29 07:16:09
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 have created, so basically I want to parse the input so each piece of data is individual. Since each row of input is inputted by the user one at a time, each time I get an entire row of input that I need to parse. Currently I am trying something like this: stringstream ss; getline(cin, ss); string name; string course; string grade; ss >> name >> course >> grade; The

Platform independent /dev/null in c++ [duplicate]

我是研究僧i 提交于 2019-11-28 23:08:29
Possible Duplicate: Implementing a no-op std::ostream Is there any stream equivalent of NULL in c++? I want to write a function that takes in a stream if the user wants to have the internal outputted to somewhere, but if not, the output goes into some fake place void data(std::stream & stream = fake_stream){ stream << "DATA" ; } i want to be able to chose to do data() or data(std::cout) Edit : Taken from @Johannes Schaub - litb's mail here with slight modifications: template<typename Ch, typename Traits = std::char_traits<Ch> > struct basic_nullbuf : std::basic_streambuf<Ch, Traits> { typedef

Is there a null std::ostream implementation in C++ or libraries?

丶灬走出姿态 提交于 2019-11-28 20:03:36
I'm looking for a std::ostream implementation that acts like /dev/null . It would just ignore anything that is streamed to it. Does such a thing exist in the standard libraries or Boost? Or do I have to roll my own? If you have boost, then there's a null ostream & istream implementation available in boost/iostreams/device/null.hpp . The gist of it: #include "boost/iostreams/stream.hpp" #include "boost/iostreams/device/null.hpp" ... boost::iostreams::stream< boost::iostreams::null_sink > nullOstream( ( boost::iostreams::null_sink() ) ); ... The simplest solution is just to use an unopened std:

C++ : friend declaration ‘declares a non-template function

让人想犯罪 __ 提交于 2019-11-28 11:37:10
I have a problem to overload the << stream operator and I don't find the solution : template<class T, unsigned int TN> class NVector { inline friend std::ostream& operator<< ( std::ostream &lhs, const NVector<T, TN> &rhs); }; template<class T, unsigned int TN> inline std::ostream& NVector<T, TN>::operator<<( std::ostream &lhs, const NVector<T, TN> &rhs) { /* SOMETHING */ return lhs; }; It produces the following error message: warning : friend declaration ‘std::ostream& operator<<(std::ostream&, const NVector&)’ declares a non-template function [-Wnon-template-friend] error: ‘std::ostream&

Writing Unicode Characters to an OStream

﹥>﹥吖頭↗ 提交于 2019-11-28 08:00:08
问题 I'm working with unicode/wide characters and I'm trying to create a toString method (Java ::toString equiv). Will ostream handle wide characters, if so is there a way to warn the consumer of the stream that it is unicode coming out of it? 回答1: Neither ostream nor the rest of C++ know anything about Unicode. Usually you write a string conversion in C++ as follows: template<typename Char, typename Traits> std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream,

The result of int c=0; cout<<c++<<c;

…衆ロ難τιáo~ 提交于 2019-11-28 01:19:43
I think it should be 01 but someone says its "undefined", any reason for that? c++ is both an increment and an assignment. When the assignment occurs (before or after other code on that line) is left up to the discretion of the compiler. It can occur after the cout << or before. This can be found in the C99 standard http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf You can find it on page 28 in the pdf or section 5.1.2.3 the actual increment of p can occur at any time between the previous sequence point and the next sequence point Since someone asked for the C++ standard (as this is a

Check if ostream object is cout or ofstream, c++

☆樱花仙子☆ 提交于 2019-11-28 00:03:26
Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to the console" << endl; return out; } } The reason I want to do this, is that I want to overload the << operator to do two different things depending on what type of stream it is used with. Is it possible to just overload the << operator twice each time with a different type of stream? Updated to reflect intention better. It's possible by checking the stream's 'identity': if (

Inheriting and overriding ostream operator in C++

扶醉桌前 提交于 2019-11-27 23:13:03
I've been trying to find an answer to this, but no one seems to have exactly the same problem as I do. I am working with several derived classes. The ostream operator << for each of these should print out some things common to each, and some things specific to each. Later on, I would like to further derive from these derived classes, and again the new derived classes need to print out some things that are in the "generations" above them. For example: The Base class .h file class Base { int FirstClassNumber; //The declaration I'm currently working with, that a friend gave me //I'm pretty sure

How to use C++ std::ostream with printf-like formatting?

[亡魂溺海] 提交于 2019-11-27 21:06:37
I am learning C++. cout is an instance of std::ostream class. How can I print formatted string with it? I still can use printf , but I want to learn more C++ style method which can take all C++ benefits. I think this should be possible with std::ostream but I can't find proper way. The only thing you can do with std::ostream directly is the well known << -syntax: int i = 0; std::cout << "this is a number: " << i; And there are various IO manipulators that can be used to influence the formatting, number of digits, etc. of integers, floating point numbers etc. However, that is not the same as

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

我与影子孤独终老i 提交于 2019-11-27 18:32:05
问题 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 have created, so basically I want to parse the input so each piece of data is individual. Since each row of input is inputted by the user one at a time, each time I get an entire row of input that I need to parse. Currently I am trying something like this: