iostream

DataContractSerializer - how can I output the xml to a string (as opposed to a file)

蓝咒 提交于 2019-12-02 17:26:26
I had a quick question regarding the datacontractserializer. Maybe it's more of a stream question. I found a piece of code that writes the xml to a filestream. I basically don't want the file and just need the string output. public static string DataContractSerializeObject<T>(T objectToSerialize) { var fs = new FileStream("test.xml", FileMode.OpenOrCreate); var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(fs, objectToSerialize); fs.Close(); return fs.ToString(); } fs.ToString() is obviously not what I'm looking for. What stream or writer etc, can I use just to

cout and cin are not functions, so what are they?

时间秒杀一切 提交于 2019-12-02 16:52:37
Generally in C programming language We consider printf and scanf to be functions. when it comes to cout and cin, in C++ what are they?I mean they cant be functions as they are not followed by parenthesis,so they are not functions. So what are cout and cin are standard input and output functions?OR something else? deepmax std::cout and std::cin are global objects of classes std::ostream and std::istream respectively, which they've overloaded operator << and >> . You should read about operator overloading . cout << expr ; ~~~~~~ ~~~~ ~~~~~~~~ object op. argument It's like a function call; the

Why std::cout instead of simply cout?

情到浓时终转凉″ 提交于 2019-12-02 14:16:42
I get these error messages for all cout and endl : main.cc:17:5: error: ‘cout’ was not declared in this scope main.cc:17:5: note: suggested alternative: /usr/include/c++/4.6/iostream:62:18: note: ‘std::cout’ After following the suggestion, everything is fine. Now I am curious, why I had to do that. We used C++ in classes before, but I never had to write a std:: before any of those commands. What might be different on this system? It seems possible your class may have been using pre-standard C++. An easy way to tell, is to look at your old programs and check, do you see: #include <iostream.h>

boost::iostream::copy(), inputstream and outstream output explanantion

二次信任 提交于 2019-12-02 14:11:28
I have a txt file: gcc-4.7.2.txt : with the data written: Hello This is a test file. Thanks :compressed as gcc-4.7.2.tar.bz2 Now, I run the following code: #include <sstream> #include <fstream> #include <iostream> #include <boost/iostreams/filtering_streambuf.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/filter/bzip2.hpp> #include <boost/filesystem.hpp> int main() { using namespace std; using namespace boost::iostreams; char filename[] = "gcc-4.7.2.tar.bz2"; if (!boost::filesystem::exists(filename)) { cout << "Can't find " << filename << ". Expect errors to follow! " <<

read equivalent for filebuf?

…衆ロ難τιáo~ 提交于 2019-12-02 13:33:17
typedef struct { char c[20]; int i; } header; void foo(std::string s) { std::ifstream ifs(s.c_str(), std::ios_base::binary | std::ios_base::in); if (ifs) { std::filebuf* pbuf = ifs.rdbuf(); pbuf->pubseekpos(std::ios_base::beg); // ... ? } } I'm trying to read a file in binary mode the C++ way, I can't identify the right method to call for reading the bits from the file, there is a read for ifstream but if I'm supposed to use that method, what is the point of filebuf ? To simplify things I have a struct that represents the order, the size and the fields of the header and I hope that there will

What is IO Stream Buffering?

不羁的心 提交于 2019-12-02 11:37:35
问题 I am unable to find the underlying concept of IO Stream Buffering and what does it mean. Any tutorials and links will be helpful. 回答1: Buffering is a fundamental part of software that handles input and output. The buffer holds data that is in between the software interface and the hardware interface, since hardware and software run at different speeds. A component which produces data can put it into a buffer, and later the buffer is "flushed" by sending the collected data to the next

Output error when input isn't a number. C++

元气小坏坏 提交于 2019-12-02 08:37:16
I am making a function that takes a number from the user's input and finds the absolute value of it. I want to make it return an error if the user inputs anything other than just a number. How would I go about doing that? (I know that this is probably an easy question for a lot of you, but I'm taking my first programming class in C++ so I know very little. Any help would be greatly appreciated.) If you are actually trying to program in idiomatic C++, ignore the (intentionally?) bad advice you are being given. Especially the answers pointing you toward C functions. C++ may be largely backwards

Writing a manipulator for a custom stream class

好久不见. 提交于 2019-12-02 07:27:01
问题 I've written a custom stream class that outputs indented text and that has manipulators that can change the indent level. All of the indenting work is implemented in a custom stream buffer class, which is used by the stream class. The buffer is working (i.e. text is indented in the output), but I can't get my manipulators to work. I was reading in a lot of places how ostream (which my class extends) overloads the operator<< like this: ostream& ostream::operator << ( ostream& (*op)(ostream&))

writing binary data (std::string) to an std::ofstream?

谁都会走 提交于 2019-12-02 04:36:48
问题 I have an std::string object containing binary data which I need to write to a file. Can ofstream f("name"); f << s; be problematic in any way? I need to read the data back exactly as it was originally. I can of course use fwrite(s.c_str(), s.size(), 1, filep) , are there any pros / cons to either method? 回答1: You should be ok as long as you open the ofstream for binary access. ofstream f("name", ios::binary | ios::out); f << s; Don't forget to open your file in binary mode when reading the

What is IO Stream Buffering?

二次信任 提交于 2019-12-02 04:32:29
I am unable to find the underlying concept of IO Stream Buffering and what does it mean. Any tutorials and links will be helpful. Buffering is a fundamental part of software that handles input and output. The buffer holds data that is in between the software interface and the hardware interface, since hardware and software run at different speeds. A component which produces data can put it into a buffer, and later the buffer is "flushed" by sending the collected data to the next component. Likewise the other component may be "waiting on the buffer" until a complete piece of data, or enough