iostream

How can I override an C++ standard-library class function?

半城伤御伤魂 提交于 2019-12-04 20:04:30
问题 How can I override a C++ standard-library class function? In my application, I use ofstream objects in many different places of code. And now I want to open files in a different permission mode in Linux, Ubuntu. But open function of ofstream has no parameter to specify the permission mode of the file it creats. Now I want to override open() function of ofstream class so it will get a parameter to specify the permissions for user access. 回答1: For starters, to clarify your terminology, the STL

How are iostream objects cin, cout, cerr, and clog implemented?

别等时光非礼了梦想. 提交于 2019-12-04 19:26:12
iostream objects cin, cout, cerr, and clog are objects declared in the iostream header. I'm aware that it's possible in some compilers to attempt to use these iostream objects before they are constructed, so under some circumstances they must be subject to the "static initialisation order fiasco". In those compilers where it's always safe to use std::cout et al, how do these objects actually get constructed? Does it involve under-the-hood compiler magic or could it in principle all be done with standard C++? std::cout etc seem to be either global variables or singleton: why are global

How to use a QFile with std::iostream?

时间秒杀一切 提交于 2019-12-04 19:13:57
问题 Is it possible to use a QFile like a std::iostream? I'm quite sure there must be a wrapper out there. The question is where? I have another libs, which requires a std::istream as input parameter, but in my program i only have a QFile at this point. 回答1: I came up with my own solution (which uses the same idea Stephen Chu suggested) #include <iostream> #include <fstream> #include <cstdio> #include <QtCore> using namespace std; void externalLibFunction(istream & input_stream) { copy(istream

iostream clear()

谁说我不能喝 提交于 2019-12-04 18:12:05
istream &func(istream &in) { string data; while (in >> data, !in.eof()) { if (in.bad()) throw runtime_error("IO stream corrupted"); if (in.fail()) { cerr << "bad data, try again" << endl; in.clear(); in.ignore(200); continue; } cout << data << endl;; } //in.clear(istream::eofbit | istream::failbit); in.clear(); return in; } why in.clear(istream::eofbit | istream::failbit); can not reset the cin? but in.clear can make it. PS: I use this function in main(), and use cin as its parameter. clear is defined like this: void clear(iostate state = goodbit); So, effectively, in.clear(); is doing this:

Should I switch to C++ I/O streams? [closed]

时光毁灭记忆、已成空白 提交于 2019-12-04 17:31:41
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I've never been much to use C++ I/O streams and have always opted for what I know. i.e. the printf functions. I know there are some

[C++] IO Stream

╄→尐↘猪︶ㄣ 提交于 2019-12-04 16:13:06
目录 输入输出流 初识输入输出 IO 类 IO 对象无拷贝或赋值 cin 流的状态 管理输出缓冲 文件输入输出 文件模式 string 流 使用 istringstream 使用 ostringstream 输入输出流 初识输入输出 cin 是 istream 类型的对象 cout、cerr、clog 是 ostream 类型的对象 向流写入数据 << 运算符接受两个运算对象:左侧的运算对象必须是一个 ostream 对象,右侧的对象是要打印的值。 endl 操纵符:写入 endl 的效果是结束当前行,并且将与设备相关联的缓冲区中的数据刷到设备中。缓冲区刷新操作可以保证到目前为止所产生的所有输出都真正写入输出流中,而不是仅停留在内存中等待写入流。 从流读取数据 int v1 = 0, v2 = 0; std::cin >> v1 >> v2; 输入运算符 >> :接受一个istream作为其左侧运算对象,接受一个对象作为其右侧运算对象。从给定的istream读入数据,并存入给定的对象中。 使用 getline 读取一整行 getline 函数的参数是一个输入流和一个 string 对象,函数从给定的输入流中读入内容,直到遇到换行符为止( 换行符本身也被读取 ),然后报所读到的内容保存到string对象中( 不保存换行符 )。getline只要遇到换行符就结束读取操作,并返回结果

Is there a good idiom to deal with alternative output streams?

旧街凉风 提交于 2019-12-04 15:39:59
I want to write a simple program that depending on the options passed it the executable will print the output to the screen or to a file. The program is simple. #include<iostream> int main(int argc, char* argv[]){ ... process options... std::ostream& out = ... // maybe std::cout, maybe a *new* std::ofstream; out << "content\n"; } Is there a good idiom to make out refer alternatively to std::cout or a file stream at runtime? I tried with pointers, but it is horrible. I couldn't avoid using pointers (Not to mention that more ugly code is needed to delete the pointer later). #include<iostream>

Does using ignore(numeric_limits<streamsize>::max()) in the IOStreams library handle arbitrarily massive streams?

我们两清 提交于 2019-12-04 12:24:24
In the C++ standard (section 27.6.1.3\24), for the istream ignore() function in the IOStreams library, it implies that if you supply an argument for 'n' of numeric_limits::max() , it will continue to ignore characters forever up until the delimiter is found, even way beyond the actual max value for streamsize (i.e. the 'n' argument is interpreted as infinite). For the gcc implementation this does indeed appear to be how ignore() is implemented, but I'm still unclear as to whether this is implementation specific, or mandated by the standard. Can someone who knows this well confirm that this is

How to write a streaming 'operator<<' that can take arbitary containers (of type 'X')?

吃可爱长大的小学妹 提交于 2019-12-04 10:49:02
问题 I have a C++ class " X " which would have special meaning if a container of them were to be sent to a std::ostream . I originally implemented it specifically for std::vector<X> : std::ostream& operator << ( std::ostream &os, const std::vector<X> &c ) { // The specialized logic here expects c to be a "container" in simple // terms - only that c.begin() and c.end() return input iterators to X } If I wanted to support std::ostream << std::deque<X> or std::ostream << std::set<X> or any similar

C++ having cin read a return character

末鹿安然 提交于 2019-12-04 10:27:39
问题 I was wondering how to use cin so that if the user does not enter in any value and just pushes ENTER that cin will recognize this as valid input. 回答1: You will probably want to try std::getline : #include <iostream> #include <string> std::string line; std::getline( std::cin, line ); if( line.empty() ) ... 回答2: I find that for user input std::getline works very well. You can use it to read a line and just discard what it reads. The problem with doing things like this, // Read a number: std: