iostream

Wrapping FILE* with custom std::ostream

流过昼夜 提交于 2019-11-28 00:11:40
I have a function which works with a std::ostream . I need to support using a C file handle ( FILE* ). Should I be creating my own subclass of std::ostream which delegates to a FILE* ? As Ben Voigt points out, you want to subclass streambuf . There are pages on the University of Southern California's website which have the documentation , header , and source for a GNU implementation of a streambuf subclass ( stdiobuf ) that wraps a FILE* . It has some dependencies on the library it is a part of (GroovX), but those should be easily to remove (I would begin by removing all references to GVX

Is there a way to check if an istream was opened in binary mode?

萝らか妹 提交于 2019-11-27 23:31:22
I'm using an istream which could be stringstream, ifstream or a user-defined stream type and I need to know if, in the case of an ifstream, it was not opened in binary mode (so I can throw an exception). I have tried the following method: if ((_is.flags() & ios::binary) == 0) throw exception(...) but no exception is ever thrown. The test fails in this case because _is.flags() returns 0x201 and ios::binary is 0x20. Is there a way to find out if a stream was opened in text mode? flags() returns ios_base::fmtflags which is formatting flags, whereas binary is an ios_base::openmode flag. I'm not

How to output to the console in C++/Windows

为君一笑 提交于 2019-11-27 22:54:23
When using iostream in C++ on Linux, it displays the program output in the terminal, but in Windows, it just saves the output to a stdout.txt file. How can I, in Windows, make the output appear in the console? MSN Since you mentioned stdout.txt I google'd it to see what exactly would create a stdout.txt; normally, even with a Windows app, console output goes to the allocated console, or nowhere if one is not allocated. So, assuming you are using SDL (which is the only thing that brought up stdout.txt), you should follow the advice here . Either freopen stdout and stderr with "CON", or do the

Why do I need to include both the iostream and fstream headers to open a file

一笑奈何 提交于 2019-11-27 22:49:01
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("test.txt"); return 0; } fstream is derived from iostream, why should we include both in the code above? I removed fstream, however, there is an error with ofstream. My question is ofstream is derived from ostream, why fstream is needed to make it compile? You need to include fstream because that's where the definition of the ofstream class is. You've kind of got this backwards: since ofstream derives from ostream , the fstream header includes the iostream header, so you could leave out

Inheriting std::istream or equivalent

只愿长相守 提交于 2019-11-27 22:19:20
I need to bridge two libraries over a stream. QDataStream which is a stream from Qt and some function from another libraries that looks like this void read_something(istream& i); I have no control over how the QDataStream is created and I'm not allowed to change the interface of read_somthing function. The first thing I can think of is write a class that inherits istream and wraps QDataStream. Have anybody done that before? If what I thought wasn't the proper way, I wonder what is the best way to achieve that. What you should do is write a streambuf which uses the QDataStream readBytes and

Why does explicitly calling operator<< on std::cout cause unexpected output?

感情迁移 提交于 2019-11-27 22:11:56
问题 I was simply curious about what would happen if I called operator<< on std::cout explicitly because I learnt that a.operator() is exactly the same as a() . So I do it and it prints something weird: #include <iostream> using std::cout; int main() { cout.operator<<("Hello World"); } Output: 0x80486a0 Oddly, it outputs an address (the address may be different for you but it should still be an address). I'm thinking this is the address of the string so I try dereferencing it to get it to output

Android ndk-build iostream: No such file or directory

谁都会走 提交于 2019-11-27 21:50:14
I'm having problem with compiling cpp file using ndk-build tool (windows 7 with cygwin) Error appears when I try to compile cpp file with #include: jni/native.cpp:5:20: error: iostream: No such file or directory Here is my cpp file: #include <jni.h> #include <string.h> #include <stdio.h> #include <android/log.h> #include <iostream> #define DEBUG_TAG "NDK_SampleActivity" #define LOG_TAG "hellojni" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) #ifdef __cplusplus extern "C" { #endif void

Using boost::iostreams::tee_device?

旧时模样 提交于 2019-11-27 21:37:49
Can someone help me? I am trying to do something like the following: #include <boost/iostreams/tee.hpp> #include <boost/iostreams/stream.hpp> #include <sstream> #include <cassert> namespace io = boost::iostreams; typedef io::stream<io::tee_device<std::stringstream, std::stringstream> > Tee; std::stringstream ss1, ss2; Tee my_split(ss1, ss2); // redirects to both streams my_split << "Testing"; assert(ss1.str() == "Testing" && ss1.str() == ss2.str()); But it won't compile in VC9: c:\lib\boost_current_version\boost\iostreams\stream.hpp(131) : error C2665: 'boost::iostreams::tee_device<Sink1,Sink2

generic way to print out variable name in c++

廉价感情. 提交于 2019-11-27 21:36:29
given a class struct { int a1; bool a2; ... char* a500; ... char a10000; } I want to print or stream out "a1 value is SOME_VALUE" "a2 value is SOME_VALUE" "a500 value is SOME_VALUE" ... "a10000 value is SOME_VALUE" the type of the member variables are not the same (mainly, int, bool, char*, etc, i.e., no need to overload << operator), and the member variable name could be named with anything, i.e., no rule to follow. Instead of typing explicitely one by one (very big tedious, and error-prone work), is there any generic way? Thanks for any comments! The feature you're looking for is typically

std::vector : cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

大兔子大兔子 提交于 2019-11-27 21:14:44
问题 I encountered a confusing error message when trying to do something as simple as std::cout << std::vector<int>{1,2,3}; which says cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&' int main() { std::cout << std::vector<int>{1,2,3}; } (tested using gcc-4.8.1 with -std=c++11) SO has similar questions like Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’, which is about some user defined class with nested classes. There is