Difference between cout and write in C ++

I am still confused about the difference between ostream & write (const char * s, streamsize n) in C ++ and cout in C ++ The first function writes a block of data, denoted by s, with a size of n characters, to the output buffer. Characters are written sequentially until n is written. whereas cout is an object of class ostream that represents standard output. It corresponds to the ststout cstdio stream. Can anyone clearly identify the differences between the two functions.

+5
source share
7 answers
ostream& write ( const char* s , streamsize n ); 

It is an unformatted output function , and what is written is not necessarily a c-string , so any null character found in the s array is copied to the destination and the process does not end.

cout is an object of the ostream class that represents a standard output stream.
It can write characters as formatted data , using, for example, the insert operator ostream::operator<< or as unformatted data using the write member function.

+14
source

You ask, what is the difference between a member function of a class and an instance of a class? cout is ostream and has a write() method.

As for the difference between cout << "Some string" and cout.write("Some string", 11) : it does the same, << can be a little slower since write() can be optimized because it knows the length lines in advance. On the other hand, << looks beautiful and can be used with many types, such as numbers. You can write cout << 5; but not cout.write(5) .

+6
source

cout is not a function. As you said, this is an object of class ostream. And as an object of this class, it has a write function that can be called as follows:

 cout.write(source,size); 
+1
source

"In binary files, for inputting and outputting data using the extract and insert operators (<<and →) and functions such as getline, it’s inefficient because we don’t need to format the data and the data may not be used by the separation codes used by text files, to separate elements (e.g. space, newline, etc.).

File streams include two member functions that are specifically designed to input and output binary data sequentially: write and read. The first (write) is an ostream member function inherited from the stream. And read is an istream member function that is inherited by ifstream. Objects of the fstream class have both members. Their prototypes:

write (memory_block, size); read (memory_block, size); "

from: http://www.cplusplus.com/doc/tutorial/files/

+1
source

There is no ostream& write ( const char* s , streamsize n ) function ostream& write ( const char* s , streamsize n ) . Perhaps you mean the member function ostream& ostream::write ( const char* s , streamsize n ) ?

The .write() function is called raw (or unformatted) output. It simply outputs a sequence of bytes into the stream.

The global variable cout is one instance of the ofstream class and has a .write() method. However, cout usually used for formatted output, for example:

 string username = "Poulami"; cout << "Username: '" << username << "'." << endl; 

Many different types have ostream& operator<<(ostream& stream, const UserDefinedType& data) , which can be overloaded to enrich the ofstream dictionary.

0
source

I agreed with Save Alok ! I used to look for a problem and carefully read the answer.

Perhaps in another word, cout is an ostream object, but write is just a function. Thus, cout has three ways to use encoders: one as a member function, the other is used by the operator (<<).

0
source

Oh boy! An opportunity to break a question.

From your question, I feel like you are a Java or Python programmer and definitely not a newbie.

You don’t understand that C ++ is probably the only language that allows programmers to implement primitives built into statements as members of a class and as part of a common interface.

In Java, you could never go

  class Money { int operator + (int cash) { return this.cash + cash; } void operator << () { System.out.println(cash); } int cash; } public class Main_ { public static void Main(String [] args) { Money cashOnHand; System << cashOnHand; } } 

But cpp allows this with great effect. The std :: ostream class implements stream operators, but also implements a regular write function that performs raw binary operations.

0
source

All Articles