iostream

Wrapping a commandline program with pstream

試著忘記壹切 提交于 2019-12-06 06:50:37
I want to be able to read and write to a program from C++. It seems like pstream can do the job, but I find the documentation difficult to understand and have not yet find an example. I have setup the following minimum working example. This opens python, which in turn (1) prints hello (2) ask input, and (3) prints hello2 : #include <iostream> #include <cstdio> #include "pstream.h" using namespace std; int main(){ std::cout << "start"; redi::pstream proc(R"(python -c "if 1: print 'hello' raw_input() print 'hello2' ")"); std::string line; //std::cout.flush(); while (std::getline(proc.out(), line

ofstream odd behavior

梦想与她 提交于 2019-12-06 06:18:33
问题 I've come across an odd behavior of the ofstream, 'least odd to me. Here's my program, i'm using Visual Studio 2010 Express Edition. int main () { std::ofstream file("file.txt"); file << "something1"; file.close(); file.open("file.txt", std::ios::ate | std::ios::in ); file << "something2"; file.close(); return 0; } This produces the correct output. something1something2 Now if i replace the 9th line with the following code, file.open("file.txt", std::ios::ate); i get this output. something2

Xcode 11.1: iostream' file not found

人盡茶涼 提交于 2019-12-06 06:02:05
I just updated my MacBook Pro to macOS Catalina 10.15 , and tried to compile and run a C++ command line program , but I had a problem which didn’t exist on previous versions; This is simply the code: #include <iostream> using namespace std; int main() { cout << "Hello, World!\n"; return 0; } The code compiles and outputs the expected, but still the Xcode says: fatal error: 'iostream' file not found I tried changing the Build Settings/C++ Standard Library to libstdc++ , but a warning says: warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use

Why does std::setprecision(6) stream more than six digits in fixed-width mode?

删除回忆录丶 提交于 2019-12-06 04:56:43
问题 The output of the following code: #include <limits> #include <iostream> #include <iomanip> #include <limits> #include <string> #include <sstream> using namespace std; inline string lexical_cast(const float arg) { stringstream ss; ss << fixed << setprecision(numeric_limits<float>::digits10) << arg; if (!ss) throw "Conversion failed"; return ss.str(); } int main() { cout << numeric_limits<float>::digits10 << '\n'; cout << lexical_cast(32.123456789) << '\n'; } is: 6 32.123455 I expected, and

Reading UTF-8 text and converting to UTF-16 using standard C++ wifstream

旧巷老猫 提交于 2019-12-06 04:28:08
问题 I'd like to read some text from a file that uses UTF-8 encoding and convert it to UTF-16, using std::wifstream , something like this: // // Read UTF-8 text and convert to UTF-16 // std::wifstream src; src.imbue(std::locale("???")); // UTF-8 ??? src.open("some_text_file_using_utf8"); std::wstring line; // UTF-16 string while (std::getline(src, line)) { ... do something processing the UTF-16 string ... } Is there a standard locale name for the UTF-8 conversion? Is it possible to achieve that

Are std::showbase and std::showpos mutually exclusive?

吃可爱长大的小学妹 提交于 2019-12-06 03:58:18
问题 This question arose from a discussion I was having about the correct way to output a numeric value using the usual ostream & operator << (ostream &, some_type) for a numeric type in C++. The way I'm familiar with the behavior of std::showbase and std::showpos in each base, they are basically mutually exclusive. That is: in decimal no base is shown, and the '+' is added on positive numbers; whereas in hexadecimal or octal, the base is shown, but a '+' is not shown (nor is a minus), as the

Reassigning global $stdout to console - ruby

心已入冬 提交于 2019-12-06 03:39:58
问题 I am trying to set $stdout to write to a file temporarily and then back to a file. test.rb : old_stdout = $stdout $stdout.reopen("mytestfile.out",'w+') puts "this goes in mytestfile" $stdout= old_stdout puts "this should be on the console" $stdout.reopen("mytestfile1.out",'w+') puts "this goes in mytestfile1:" $stdout = old_stdout puts "this should be back on the console" Here is the output. ruby test.rb => no output on the console cat mytestfile.out this goes in mytestfile this should be on

inheriting ostream and streambuf problem with xsputn and overflow

∥☆過路亽.° 提交于 2019-12-06 02:47:44
问题 I have been doing research on creating my own ostream and along with that a streambuf to handle the buffer for my ostream. I actually have most of it working, I can insert (<<) into my stream and get strings no problem. I do this by implimenting the virtual function xsputn. However if I input (<<) a float or an int to the stream instead of a string xsputn never gets called. I have walked through the code and I see that the stream is calling do_put, then f_put which eventually tries to put the

How to load LLVM bitcode file from an ifstream?

若如初见. 提交于 2019-12-06 02:36:36
问题 I'm trying to load an LLVM module defined in a .bc file at runtime but have run into a snag. The bitcode of interest has been generated from hello.cpp : // hello.cpp // build with: // clang-3.4 -c -emit-llvm hello.cpp -o hello.bc #include <iostream> void hello() { std::cout << "Hello, world!" << std::endl; } When the program below attempts to load it at runtime, it crashes inside llvm::BitstreamCursor::Read() : // main.cpp // build with: // g++ main.cpp `llvm-config-3.4 --cppflags --ldflags -

using stream operator<< with std::endl in c++

一个人想着一个人 提交于 2019-12-06 00:44:06
I am trying out the following C++ class for using the stream operator << to log contents from this answer : class Log { public: Log() : m_filename( "dafault.log" ) {} // if you wanna give other names eventually... Log( const std::string & p_filename ) : m_filename( p_filename ) {} virtual ~Log() { // implement your writeToFile() with std::ofstream writeToFile( m_filename, m_stream, true ); } template< typename T > Log & operator<<( const T & p_value ) { m_stream << p_value; return *this; } private: std::string m_filename; std::ostringstream m_stream; }; This works for many cases. However, it