No. ostream "outputs" from arbitrary types to character sequences, and istream "inputs" to arbitrary types from character sequences, just as described.
Yes, you can create any stream you want. Since a stream is a flow of data with a source and a sink, you typically want to use either of the following: std::stringstream - add data yourself, access it in stream form std::{i,o}fstream - data comes from / goes to a...
c++,hash,unordered-map,ostream
As far as I am aware, there is no standard interface for exposing the hash value (as opposed to the hashing function) from std::unordered_map. As you have seen, dereferencing a std::unordered_map<Key,V>::iterator yields something convertible to std::unordered_map<Key,V>::value_type, which in turn is a std::pair<const Key,V> representing a (key,value) pair, rather than a...
c++,templates,namespaces,ostream
It should be: template <typename V, typename W> std::ostream &operator<< (std::ostream& os, typename Foo<V,W>::template Bar<V,W> &b) {return os;} but changing the class may be simpler: template <typename X, typename Y> class Foo { public: class Bar { X a; Y b; }; Bar baba; }; template <typename V, typename W>...
c++,operator-overloading,ostream
if you want your object to work in both cases cout << p ; cout << *p; you have to overload your operator for pointers also, so you can add this to your code and keep the old one: friend ostream& operator<<(ostream& os, const Person* p); ostream& operator<<(ostream& os, const...
From the description it seems you just want to build a string and process this. You can do so using string streams: #include <iostream> #include <sstream> void f(std::ostream& out) { out << "f()"; } int main() { std::ostringstream out; f(out); std::cout << "string='" << out.str() << "'\n"; f(std::cout); } If...
c++,templates,c++11,ostream,enable-if
You get an ambiguous overload for 'operator<< error when the type is std::string, because the templated version in your code has an equal precedence with the one shipped in the ostream header. You can check that this is the source of your problem by changing your test program with this:...
Use ReadLine and find the line you wanna replace, and use replace to replace the thing you wanna replace. For example write: string Example = "Text to find"; openFile="C:\\accounts.txt"; // the path of the file ReadFile(openFile, Example); OR #include <fstream> #include <iostream> #include <string> int main() { ifstream openFile; string...
c++,c++11,iostream,ostream,istream
Here is what the standard says about std::basic_istream<...>::read() in 27.7.2.3 [istream.unformatted] paragraphs 30 and 31 (emphasis is mine): basic_istream<charT,traits>& read(char_type* s, streamsize n); Effects: Behaves as an unformatted input function (as described in 27.7.2.3, paragraph 1). After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception,...
c++,arrays,printing,hashtable,ostream
There is a logic error in the function Customer::getHash. That may not solve your problem but it should be fixed anyway. int Customer::getHash(int hash) { string key = getLastname(); cout<<"key: "<<key<<endl; // getFirstname(); // getID(); int i = 0; // int j = 0; // int k = 0; for...
c++,string,vector,iterator,ostream
That code is only needed if you want to add a delimiter after every inserted integer, but even then it doesn't have to be that complicated. A simple loop and the use of to_string is far more readable: std::string str; for (int i = 0; i < vec.size(); ++i) {...
c++,operator-overloading,ostream
In Argument Depended Lookup (or Koenig Lookup), compiler adds to the scope of visibility all symbols declared in parent scopes of each parameter. Even if Y is "child namespace" of X, they are not related in terms of ADL. First of your parameters is type defined in std:: namespace, while...
Seems like my suspicions turned out to be true. I modified the code like so: #include <iostream> using namespace std; #include <Windows.h> void crash() { printf("%i\n", GetCurrentThreadId()); system("PAUSE"); } int main() { printf("%i\n", GetCurrentThreadId()); atexit(crash); //while(true); return 0; } When the program exists normally both printf()s display identical thread IDs,...
c++,linux,pipe,ostream,istream
Victory! Thanks Barmar! There are two problems with the code above; the first one is embarrassing. The writer complaints go into a logfile, and I totally looked in the wrong logfile. The correct logfile was full of complaints. I can't use tellp on a pipe stream. So, it works if...
c++,dictionary,containers,overloading,ostream
Your operator<< for stack is popping all of the elements off of the stack, so the stack is empty by the time you make a copy of it to put in the map. To demonstrate, if I change your main() to this: int main() { stack<int> s2; s2.push(1); s2.push(2); cout...
c++,stream,cout,outputstream,ostream
[basic.stc.static]/1: All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program ...
You are using the operator<<(ostream&, const char *) overload for something that is plainly not a C-style null-terminated string, which results in undefined behavior.1 In the case of 40 2C 00 00, the function interpreted the zero-byte as a null terminator, so it only printed the first two bytes. When...
c++,qt,fstream,ifstream,ostream
You are passing a std::string to the std::ofstream constructor. This is a C++11 feature and to use this you need to pass -std=c++11 to GCC or Clang. MSVC automatically compiles for its hybrid not-quite-C++11-or-anything-else language that the compiler release compiles. If you're using Qt 5's qmake, you can do just...
fooprint(out) is not the creation of a temporary but rather the declaration of a variable of type fooprint with the name provided as the argument to the macro. In order to not make this a declaration, and instead an expression, two quick changes you can make are surrounding it in...
since nobody is posting correct answer to show that this has been solved Your function void display(std::ostream& os) const; should return something to print out; for example char * char * display(std::ostream& os) const; when using << operator, the function you are using toStrWithStyle should return something to be printed...
The benefit of switching to the ostream version is that in the case you later need to print to other places besides std::cout then you can do it with the same function implementation, whereas in this moment if you want to print to a file you would need to use...
c++,templates,inheritance,ostream
Replace template <type S> friend std::ostream& operator<<(std::ostream& out, const derived<S>& D) { return (D.print(out)); } with friend std::ostream& operator<<(std::ostream& out, const derived<T>& D) { return (D.print(out)); } error will go away. With earlier definition, you are trying to define a new template function with same signature....
c++,pointers,implicit-conversion,ostream
Read this (your question is answered in the very last section, "The safe bool problem"). To elaborate a bit, the implementation defines an implicit conversion to void* defined for things like std::cin and std::cout, just so that code like while(std::cin>>x){...} compiles, while code like int x = std::cin; doesn't. It's...
c++,namespaces,operator-overloading,ostream
Question: is this the correct way of solving this problem? Answer: Yes, it is the correct way of solving the problem. Question: Isn't this approach giving away details of the implementation? Answer: Not at all. The line friend std::ostream& operator <<(std::ostream& output, const Bar&); declares the function as an external...
You need to overload operator<< for your class, because that's what ostream_iterator calls when being assigned to. Something like this: std::ostream& operator<<(std::ostream& os, const Student& s) { return os << get_name() << get_age() << get_marks(); // needs some formatting } And then you construct the iterator with Student as template...
You have three options: 1) Output the modified data to a new file; or 2) Mark records as deleted, but available for reuse; or 3) Move data "up" to the unused slots (like an array). Maybe you should be using a database instead. Many applications write the original and modified...
c++,save,operator-overloading,ostream
You are streaming Test by non-const reference: friend std::ostream& operator << (std::ostream& os, Test& p); You want to stream it by const reference: friend std::ostream& operator << (std::ostream& os, const Test& p); ^^^^^^ The error comes from the fact that when you call it from save(), you are passing in...
c++,stream,iostream,ostream,istream
It seems, you want to create something that channels the data written to Get()'s std::ostream to Put()'s std::istream. To do so you'll need to write a suitable stream which may need to deal with connecting multiple threads. If it is sufficient to read data from the stream written to by...
c++,operator-overloading,ostream
Yes, it's perfectly legal to overload operator<< while varying the return type - it just won't work properly for the common use of streaming your type alongside other types to C++ streams. As an example... Foo my_foo; std::cout << my_foo << '\n'; ...will have evaluation attempted... operator<<(std::cout, my_foo).operator<<(std::ostream& ??? '\n'...
c++,c++11,operator-overloading,ostream
std::ostream is an alias for std::basic_ostream<char>. std::wcout is of type std::basic_ostream<wchar_t>, also known as std::wostream. Note the different character types. If you want your operator<< to work with std::wcout, it should take and return std::wostream& rather than std::ostream&....
You can try to change the current position in the stream using ostream::seekp() or streambuf::pubseekpos(): std::ofstream out; out.seekp(-10, std::ios_base::cur); // move back 10 positions from current location out.rdbuf()->pubseekpos(12); // move to position 12 Keep in mind that: this might fail if the underlying destination of the data does not support...
The way you modify the behavior of std::ostream is not by overloading any of the output operators! Instead, you derive a class from std::streambuf and override the virtual functions overflow() and sync(). In you case you'd probably create a filtering stream buffer, i.e., you'd take another std::streambuf as argument and...
c++,operator-overloading,operator-keyword,cout,ostream
You must overload operator<< between an ostream object and your own object. ostream &operator<<(ostream &output, const projects &prj) { //print something you want of prj with output << ... return output; } Also, you need to add this friend definition to your class if the overloaded operator needs to access...
You can use ostream::seekp to move the stream position backward or forward, as described here
You cannot fix this without modifying BaseEl::display() to stop producing new line at the end of the output. In general, it is a bad idea to add endl to your own output. Let the caller do that if he needs a newline. Note that a more C++-like approach to output...