Poulami
Poulami

Reputation: 1087

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 the block of data pointed by s, with a size of n characters, into the output buffer. The characters are written sequentially until n have been written. whereas cout is an object of class ostream that represents the standard output stream. It corresponds to the cstdio stream stdout. Can anyone clearly bring out the differences between the two functions.

Upvotes: 8

Views: 17281

Answers (7)

user9599745
user9599745

Reputation:

Oh boy! A chance to smash up a question.

From your question I feel you are some Java or Python programmer and definitely not a begginner.

You dont understand that C++ is probably the only language that allows programmers to implement primitive built in operators as class members and as part of the general 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. class std::ostream implements the stream operators but also implements a regular write function which does raw binary operations.

Upvotes: 1

onebraveman
onebraveman

Reputation: 511

I agreed with Alok Save!A litte before, I searched the problem and read the answer carefully.

Maybe in other word, cout is an object of ostream, but write is just a function provided. So cout have twe ways to used by coders: one is as a member function, another is used by operator(<<).

Upvotes: 0

Alok Save
Alok Save

Reputation: 206546

ostream& write ( const char* s , streamsize n );

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

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

Upvotes: 18

Andr&#233; Caron
Andr&#233; Caron

Reputation: 45224

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

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

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

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

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

Upvotes: 1

John Humphreys
John Humphreys

Reputation: 39294

"In binary files, to input and output data with the extraction and insertion operators (<< and >>) and functions like getline is not efficient, since we do not need to format any data, and data may not use the separation codes used by text files to separate elements (like space, newline, etc...).

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

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

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

Upvotes: 1

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67157

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

As to the difference between cout << "Some string" and cout.write("Some string", 11): It does the same, << might be a tiny bit slower since write() can be optimized as it knows the length of the string in advance. On the other hand, << looks nice and can be used with many types, such as numbers. You can write cout << 5;, but not cout.write(5).

Upvotes: 8

Benjamin Lindley
Benjamin Lindley

Reputation: 103713

cout is not a function. Like you said, it is an object of class ostream. And as an object of that class, it possesses the write function, which can be called like this:

cout.write(source,size);

Upvotes: 2

Related Questions