iostream

Unexpected results with std::ofstream binary write

白昼怎懂夜的黑 提交于 2019-11-28 13:15:34
I'm new to C++ std::stream and I'm making some tests. I have this simple code: int i = 10; char c = 'c'; float f = 30.40f; std::ofstream out("test.txt", std::ios::binary | std::ios::out); if(out.is_open()) { out<<i<<c<<f; out.close(); } As the stream is opened as std::ios::binary I expect in the test.txt file to have the binary representation of i , c and f , but instead I have 10c30.4 . Can you please tell me what I'm doing wrong? std::ios::binary promises to not do any line-end conversions on the stream (and some other small behavioral differences with text streams). You could look at Boost

Is it possible to define enumalpha?

久未见 提交于 2019-11-28 13:01:05
I would like to be able to write: cout << enumalpha << Monday; and get printed on console: Monday P.S. Monday is an enum type. Okay, let's go all preprocessor then :) Intended way of use: DEFINE_ENUM(DayOfWeek, (Monday)(Tuesday)(Wednesday) (Thursday)(Friday)(Saturday)(Sunday)) int main(int argc, char* argv[]) { DayOfWeek_t i = DayOfWeek::Monday; std::cout << i << std::endl; // prints Monday std::cin >> i >> std::endl; // reads the content of a string and // deduce the corresponding enum value } Dark magic, involving the helpful Boost.Preprocessor library. #include <boost/preprocessor/cat.hpp>

While loop with try catch fails at bad cin input

柔情痞子 提交于 2019-11-28 12:46:21
I can't seem to figure out why this falls into a loop after getting non-int input. I've tried cin.flush(), which doesn't seem to exist, cin.clear(), which seems like it should work, even cin.sync() after reading someone else post about it working, but didn't seem to make much sense. Also tried cin.bad(). Thank you very much for any help Please enter the first number: f Sorry, I don't think that's a number? Please enter the first number: Sorry, I don't think that's a number? Please enter the first number: Sorry, I don't think that's a number? Please enter the first number: Sorry, I don't think

C++ cin.fail() question

╄→гoц情女王★ 提交于 2019-11-28 12:42:44
When running the following code and enter a number, it works fine. But when entering a letter, the program enters an infinite loop, displaying "Enter a number (0 to exit): cin failed." My intent was to handle the cin fail case and prompt the user again. int number; do{ cout << "Enter a number (0 to exit): "; cin >> number; if(cin.fail()){ cout << "cin failed." << endl; cin.clear(); }else{ cout << "cin succeeded, " << number << " entered." << endl; } }while(number != 0); You need to clear the line from cin , using cin.ignore , in addition to clearing the stream state (which is what cin.clear

Why does writing to temporary stream fail?

不想你离开。 提交于 2019-11-28 12:18:06
Consider the following code: #include <sstream> #include <iostream> class Foo : public std::stringstream { public: ~Foo() { std::cout << str(); } }; int main() { Foo foo; foo << "Test1" << std::endl; Foo() << "Test2" << std::endl; return 0; } When I execute this, it gives me: 004177FC Test1 I do not understand why the second example gives me gibberish. The temporary should live until the entire expression is evaluated, so why does it not behave the same as the first example? I tested it. I can guess that operator<< cannot bind a temporary to a non-const reference, so any externally defined

cannot access private members in friend ostream

一世执手 提交于 2019-11-28 12:11:35
问题 I tried to make friend ostream function. The compiler say i cannot access private member of the class, even though i declared it as friend . I read a similar question and it say the problem is with the namespcaes.(the question: C++ friend function can't access private members) My Code is below: header: #include <iostream> #include <string> //using namespace std; namespace biumath { #ifndef BIUMATH_H #define BIUMATH_H class Assignment { private: int **m_varArray; int m_rowsOfVarArray; public:

bidirectional iterator over file/ifstream

半世苍凉 提交于 2019-11-28 11:06:29
I need an input file stream which would have a bidirectional iterator/adapter. Unfortunately std::ifstream (and similar) can be used only with std::istream_iterator which is a kind of forward iterator which cannot go backwards. (or am I mistaken here?) I could simply load the whole file to memory and then use a much more powerful random-access iterator over the array; however I would like to avoid that, and read only as much as I really need. It may happen that I really need only a small portion of a file. I could somehow do it manually using C stdio.h functions, but that will be painful. I

Using boost::asio::async_read with stdin?

北城以北 提交于 2019-11-28 10:22:45
short question: I have a realtime-simulation which is running as a backround process and is connected with pipes to the calling pogramm. I want to send commands to that process using stdin to get certain information from it via stdout. Now because it is a real-time process, it has to be a non blocking input. Is boost::asio::async_read in conjunction with iostream::cin a good idea for this task? how would I use that function if it is feasible? Any more suggestions? Sam Miller Look at boost::asio::posix::stream_descriptor http://www.boost.org/doc/libs/release/doc/html/boost_asio/example/cpp03

Effective use of C++ iomanip library

感情迁移 提交于 2019-11-28 10:17:07
I created a Vector class in C++ and it works great for my problems. I am now cleaning it up, and I ran into the following piece of code: std::ostream& operator<<(std::ostream &output, const Vector &v){ output<<"[" <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._x<<", " <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._y<<", " <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._z<<"]"; return output; } The code allows to print a vector

UTF-8-compliant IOstreams

痞子三分冷 提交于 2019-11-28 10:09:30
问题 Does GCC's standard library or Boost or any other library implement iostream-compliant versions of ifstream or ofstream that supports conversion between UTF-8-encoded (file-) streams and a std::vector<wchar_t> or std::wstring ? 回答1: Your question doesn't quite work. UTF-8 is a specific encoding, while wchar_t is a data type. Moreover, wchar_t is intended by the standard to represent the system's character set , but this is entirely left to platform, and the standard makes no requirements.