iostream

How can I derive my own stream from a standard stream?

倖福魔咒の 提交于 2019-12-30 09:09:03
问题 How can I derive my own stream from a standard stream? In C# language, there is a Stream class, but C++'s streams are too complex. I want something like this: class my_stream : public std::stream { // How to derive? }; void using_a_stream(std::stream* s) { *s << "Hello world"; } void main() { std::stream s1; std::fstream s2("C:\\test.txt"); my_stream s3; using_a_stream(&s1); using_a_stream(&s2); using_a_stream(&s3); } Note: The code just a sample and may be invalid C++ program. Thanks. 回答1: I

How to debug segmentation fault?

冷暖自知 提交于 2019-12-30 05:27:33
问题 It works when, in the loop, I set every element to 0 or to entry_count-1. It works when I set it up so that entry_count is small, and I write it by hand instead of by loop (sorted_order[0] = 0; sorted_order[1] = 1; ... etc). Please do not tell me what to do to fix my code. I will not be using smart pointers or vectors for very specific reasons. Instead focus on the question: What sort of conditions can cause this segfault? Thank you. ---- OLD ----- I am trying to debug code that isn't working

iostream and large file support

倖福魔咒の 提交于 2019-12-30 04:38:06
问题 I'm trying to find a definitive answer and can't, so I'm hoping someone might know. I'm developing a C++ app using GCC 4.x on Linux (32-bit OS). This app needs to be able to read files > 2GB in size. I would really like to use iostream stuff vs. FILE pointers, but I can't find if the large file #defines (_LARGEFILE_SOURCE, _LARGEFILE64_SOURCE, _FILE_OFFSET_BITS=64) have any effect on the iostream headers. I'm compiling on a 32-bit system. Any pointers would be helpful. 回答1: This has already

cstdio streams vs iostream streams?

ぐ巨炮叔叔 提交于 2019-12-30 01:37:05
问题 I just learned of the existence of the ios_base::sync_with_stdio function, which basically allows you to turn off (or on if you already turned it off) the synchronization between iostream streams that are used in C++ and the cstdio streams that are part of Standard C. Now, I always thought that stdout , stderr and stdin in C were essentially wrapped in a set of objects in C++ in the iostreams classes. But if they have to be synchronized with each other, this would indicate that C++'s iostream

Does anyone actually use stream extraction operators?

ε祈祈猫儿з 提交于 2019-12-30 00:03:10
问题 I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful. I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string ). Are these appropriate only for short example programs and textbooks? Is operator>> a failed feature of C++? Questions have been asked about safely overloading stream operators. What I wonder is if anyone does this in practice. Even for

How to read from std::cin until the end of the stream?

大城市里の小女人 提交于 2019-12-29 09:05:54
问题 My problem is, that I want to read the input from std::cin but don't know how long the input is. Also I have to char and can't use std::string . There are two ways I have to handle: a) The user inputs text and when he hits [ENTER] the program stops reading. b) The user redirects std::cin to a file (like .\a.oput < file ) which can hold multiple lines. Edit: Just noticed that std::cin.eof() is always false also in the case of reading form a file. For a) I could read until \n occures. For b)

cin.eof() functionality

心已入冬 提交于 2019-12-29 08:33:11
问题 I understand that cin.eof() tests the stream format. And while giving input, end of character is not reached when there is wrong in the input. I tested this on my MSV C++ 2010 and am not understanding the strange results. No matter what I give the input, I am getting Format Error message that is present in the program. #include <iostream> using namespace std; int main() { int i; cin>> i; if(!cin.eof()) { cout<< "\n Format Error \n"; } else { cout<< "\n Correct Input \n"; } getchar(); return 0

How to use cin.fail() in c++ properly

安稳与你 提交于 2019-12-29 07:12:28
问题 I'm writing a program where I get an integer input from the user with cin>>iUserSel; . If the user puts in a letter, the program goes to an infinite loop. I tried to prevent that with the code below, but the program goes to an infinite loop and prints out "Wrong! Enter a #!". How can I fix my program? cin>>iUserSel; while (iValid == 1) { if (cin.fail()) { cin.ignore(); cout<<"Wrong! Enter a #!"<<endl; cin>>iUserSel; }//closes if else iValid = 0; }//closes while I found some information on

C++PrimerPlus(第6版)中文版 第3章 处理数据 编程练习 参考答案

爷,独闯天下 提交于 2019-12-28 16:22:12
自己编写的参考答案,在VS2019中都可以编译通过,不是标准答案,也不是最优答案,仅供参考 1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。 #include < iostream > using namespace std; int main() { const int FtToIn = 12; cout << “请输入你的身高(英寸):___\b\b\b”; int in01; cin >> in01; int ft = in01 / FtToIn; int in02= in01 % FtToIn; cout << “你的身高为” << ft << “英尺” << in02 << “英寸”; } 2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用三个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方

redirect std::cout to a custom writer

喜欢而已 提交于 2019-12-28 11:57:31
问题 I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog << "This will appear in oss!" << std::flush; std::cout << oss.str() << '\\n'; // Give clog back its previous buffer std::clog.rdbuf(former_buff); return 0; } so, in a mainloop, I will do