iostream

trying to write std:out and file at the same time

社会主义新天地 提交于 2019-12-01 06:47:32
问题 I am trying to write to file and stdout at the same time within c++ by overloading ofstream test.h #pragma once #include <iostream> using std::ofstream; class OutputAndConsole:public ofstream { public: std::string fileName; OutputAndConsole(const std::string& fileName):ofstream(fileName),fileName(fileName){ }; template <typename T> OutputAndConsole& operator<<(T var); }; template <typename T> OutputAndConsole& OutputAndConsole::operator<<(T var) { std::cout << var; ofstream::operator << (var)

c++ extracting double from stream

十年热恋 提交于 2019-12-01 05:47:05
问题 I've a funny problem with school exercise. I am given latitude and longitude and I have to ensure that it is in right format: \(\d+\.\d+[NS], \d\+.\d+[EW]\) . So as you can see I have to check the . is there. char lBracket, rBracket, comma, NS, EW; int nLat, nLon; double lat, lon; istringstream iss ("(51.5N, 0.0E)"); iss >> fixed >> lBracket >> nLat >> lat >> NS >> comma >> nLon >> lon >> EW >> rBracket; lat += nLat; lon += nLon; the >> lon extracts ".0E" part (which is a valid double) but i

file stream tellg/tellp and gcc-4.6 is this a bug?

两盒软妹~` 提交于 2019-12-01 05:14:43
This code: #include <iostream> #include <cstdio> #include <fstream> #include <string> int main() { std::remove("test.txt"); std::fstream f("test.txt",std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc); std::cout << f.good() << std::endl; f<<"test"<< std::flush; std::cout << f.tellg() << " " << f.tellp() << std::endl; f.seekg(0); std::string s; f>>s; std::cout << f.tellg() << " " << f.tellp() << std::endl; } Gives following output in gcc-4.4.5 1 4 4 4 4 i.e. both tellg and tellp returned expected stream position 4. While gcc-4.6.0 Gives: 1 4 4 -1 4 Where can I find a reference

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

假如想象 提交于 2019-12-01 04:58:17
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. I think there are three levels of answer to this question: Level 1 : It is complicated, especially if you are

Why would I include iostream and ostream separately? [duplicate]

二次信任 提交于 2019-12-01 04:36:58
This question already has an answer here: iostream vs ostream what is different? 5 answers I've noticed that many people include iostream and ostream in C++ programs separately, like so: #include <iostream> #include <ostream> int main() { } Why would anyone do that? Since iostream inherits from ostream, it should include everything in it, right? Is there some obscure reason? What about simple (std::cout) code? Although stringstream inherits from iostream , it is not declared in the <iostream> header. The <iostream> header contains the definition of the iostream type along with the famous cout

C++ What is wrong with using a toString() method

最后都变了- 提交于 2019-12-01 04:21:37
I just came across this question which is about how to be able to print an object via std::cout << x << std::endl; As I understood, the standard way to accomplish this is to overload the ostreams << operator. However, this is adding a feature to the ostream rather than to my class. The alternative (also given as answer to the above mentioned question) is to override the string conversion operator. However, this comes with the warning of leading to "unintentional conversions and hard-to-trace bugs". Now i wonder if there are any drawbacks of writing a toString() method and then using it via std

Is it possible to “prepare” input from cin?

老子叫甜甜 提交于 2019-12-01 04:14:31
问题 In his answer, specifically in the linked Ideone example, @Nawaz shows how you can change the buffer object of cout to write to something else. This made me think of utilizing that to prepare input from cin , by filling its streambuf : #include <iostream> #include <sstream> using namespace std; int main(){ streambuf *coutbuf = cout.rdbuf(cin.rdbuf()); cout << "this goes to the input stream" << endl; string s; cin >> s; cout.rdbuf(coutbuf); cout << "after cour.rdbuf : " << s; return 0; } But

file stream tellg/tellp and gcc-4.6 is this a bug?

时间秒杀一切 提交于 2019-12-01 02:31:02
问题 This code: #include <iostream> #include <cstdio> #include <fstream> #include <string> int main() { std::remove("test.txt"); std::fstream f("test.txt",std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc); std::cout << f.good() << std::endl; f<<"test"<< std::flush; std::cout << f.tellg() << " " << f.tellp() << std::endl; f.seekg(0); std::string s; f>>s; std::cout << f.tellg() << " " << f.tellp() << std::endl; } Gives following output in gcc-4.4.5 1 4 4 4 4 i.e. both tellg and

C++ What is wrong with using a toString() method

我与影子孤独终老i 提交于 2019-12-01 02:07:32
问题 I just came across this question which is about how to be able to print an object via std::cout << x << std::endl; As I understood, the standard way to accomplish this is to overload the ostreams << operator. However, this is adding a feature to the ostream rather than to my class. The alternative (also given as answer to the above mentioned question) is to override the string conversion operator. However, this comes with the warning of leading to "unintentional conversions and hard-to-trace

C++ cout with prefix

喜你入骨 提交于 2019-12-01 01:54:55
I want a ostream with a prefix at the beginning of every line redirected on cout; I try this: #include <iostream> #include <thread> class parallel_cout : public std::ostream { public: parallel_cout(std::ostream& o):out(o){} template <typename T> std::ostream& operator<< (const T& val) { out << "prefix " << val; return *this; } std::ostream& out; }; int main() { parallel_cout pc(std::cout); pc<<"a\nb"<<"c\n"; } but i have in output prefix a b without c. why this? The way you modify the behavior of std::ostream is not by overloading any of the output operators! Instead, you derive a class from