iostream

Wrap subprocess' stdout/stderr

≯℡__Kan透↙ 提交于 2019-11-27 20:53:26
I'd like to both capture and display the output of a process that I invoke through Python's subprocess. I thought I could just pass my file-like object as named parameter stdout and stderr I can see that it accesses the fileno attribute - so it is doing something with the object. However, the write() method is never invoked. Is my approach completely off or am I just missing something? class Process(object): class StreamWrapper(object): def __init__(self, stream): self._stream = stream self._buffer = [] def _print(self, msg): print repr(self), msg def __getattr__(self, name): if not name in [

Standard no-op output stream

瘦欲@ 提交于 2019-11-27 19:37:33
Is there a way to create an ostream instance which basically doesn't do anything ? For example : std::ostream dummyStream(...); dummyStream << "Nothing will be printed"; I could just create an ostringstream, but data will be buffered (and I really don't want to make anything with them, so it adds a useless overhead). Any idea ? [edit] Found this related question which suits my needs. However, I think it could be useful to have a answer saying how to create a valid (no badbit) output stream with standard c++. You need a custom streambuf. class NullBuffer : public std::streambuf { public: int

Redirect cin to a string

让人想犯罪 __ 提交于 2019-11-27 18:14:33
问题 I want to have cin read input from a string. Is there a way to have it do this? Something like this: const char * s = "123 ab"; cin.readFrom(s);//<---- I want something like this int i; cin>>i; cout<<i; //123 回答1: Like this: #include <sstream> #include <iostream> std::istringstream stream("Some string 123"); streambuf* cin_backup = std::cin.rdbuf(stream.rdbuf()); You might want to back up the original rdbuf of std::cin, if you want to use it again. 回答2: I would recommend using a string stream

operator << must take exactly one argument

泪湿孤枕 提交于 2019-11-27 18:04:17
a.h #include "logic.h" ... class A { friend ostream& operator<<(ostream&, A&); ... }; logic.cpp #include "a.h" ... ostream& logic::operator<<(ostream& os, A& a) { ... } ... When i compile, it says: std::ostream& logic::operator<<(std::ostream&, A&)' must take exactly one argument. What is the problem? Cat Plus Plus The problem is that you define it inside the class, which a) means the second argument is implicit ( this ) and b) it will not do what you want it do, namely extend std::ostream . You have to define it as a free function: class A { /* ... */ }; std::ostream& operator<<(std::ostream&

Reading a file character by character in C

心不动则不痛 提交于 2019-11-27 17:37:19
Hey everyone, I'm writing a BF interpreter in C and I've run into a problem reading files. I used to use scanf in order to read the first string, but then you couldn't have spaces or comments in your BF code. Right now here is what I have. char *readFile(char *fileName) { FILE *file; char *code = malloc(1000 * sizeof(char)); file = fopen(fileName, "r"); do { *code++ = (char)fgetc(file); } while(*code != EOF); return code; } I know the problem arises in how I'm assigning the next char in the file to the code pointer but I'm just not sure what that is. My pointer knowledge is lacking which is

Who architected / designed C++'s IOStreams, and would it still be considered well-designed by today's standards? [closed]

六月ゝ 毕业季﹏ 提交于 2019-11-27 16:45:46
First off, it may seem that I'm asking for subjective opinions, but that's not what I'm after. I'd love to hear some well-grounded arguments on this topic. In the hope of getting some insight into how a modern streams / serialization framework ought to be designed, I recently got myself a copy of the book Standard C++ IOStreams and Locales by Angelika Langer and Klaus Kreft . I figured that if IOStreams wasn't well-designed, it wouldn't have made it into the C++ standard library in the first place. After having read various parts of this book, I am starting to have doubts if IOStreams can

How can I change the precision of printing with the stl?

被刻印的时光 ゝ 提交于 2019-11-27 16:18:24
I want to print numbers to a file using the stl with the number of decimal places, rather than overall precision. So, if I do this: int precision = 16; std::vector<double> thePoint(3); thePoint[0] = 86.3671436; thePoint[1] = -334.8866574; thePoint[2] = 24.2814; ofstream file1(tempFileName, ios::trunc); file1 << std::setprecision(precision) << thePoint[0] << "\\" << thePoint[1] << "\\" << thePoint[2] << "\\"; I'll get numbers like this: 86.36714359999999\-334.8866574\24.28140258789063 What I want is this: 86.37\-334.89\24.28 In other words, truncating at two decimal points. If I set precision

When does cout flush?

一世执手 提交于 2019-11-27 16:06:56
I know endl or calling flush() will flush it. I also know that when you call cin after cout , it flushes too. And also when the program exit. Are there other situations that cout flushes? I just wrote a simple loop, and I didn't flush it, but I can see it being printed to the screen. Why? Thanks! for (int i =0; i<399999; i++) { cout<<i<<"\n"; } Also the time for it to finish is same as with endl both about 7 seconds. for (int i =0; i<399999; i++) { cout<<i<<endl; } There is no strict rule by the standard - only that endl WILL flush, but the implementation may flush at any time it "likes". And

How to output with 3 digits after the decimal point with C++ stream?

半腔热情 提交于 2019-11-27 16:04:19
问题 Given a variable of float type, how to output it with 3 digits after the decimal point, using iostream in C++? 回答1: Use setf and precision. #include <iostream> using namespace std; int main () { double f = 3.14159; cout.setf(ios::fixed,ios::floatfield); cout.precision(3); cout << f << endl; return 0; } This prints 3.142 回答2: This one does show "13.141" #include <iostream> #include <iomanip> using namespace std; int main(){ double f = 13.14159; cout << fixed; cout << setprecision(3) << f <<

Why does std::fstream set the EOF bit the way it does?

↘锁芯ラ 提交于 2019-11-27 15:59:50
问题 I recently ran into a problem caused by using fstream::eof(). I read the following line from here: The function eof() returns true if the end of the associated input file has been reached, false otherwise. and (mistakenly) assumed this meant that if I used fstream::read() and read past the end of the file, the function eof() would tell me. So I did something like this (very generalized): for(int i = 0; i < max && !file.eof(); i++) { file.read(mything, sizeof(mything)); } The problem came