Difference between cout and cout.write in C++

In summary, the purpose of cout.write is to write a string using the public method, while cout is used with the << operator to stream any type into it. They are not exactly the same and have different functions.
  • #1
Ilikecereal
17
1
What's the purpose of cout.write. Isn't it pretty much the same thing as cout?

For eg, the following code would work the same with cout and cout.write

int main( )
{ char string[80] ;
cout<<"Enter string\n" ;
cin.getline(string, 80) ;
int x1 = strlen(string) ;
for(int i = 0 ; string != '\0' ; i++)
if(string == ' ')
string = '-' ;
cout<<"The changed string is\n" ;
cout.write(string, x1) ;
return 0 ;
}
 
Physics news on Phys.org
  • #2
cout.write() is the public method to actually write a string.

cout is used in an overloaded version of the << operator.
In particular this allows you to stream any type into cout, which is not something cout.write supports.
 

Related to Difference between cout and cout.write in C++

1. What is the difference between cout and cout.write in C++?

Both cout and cout.write are used for output in C++. However, cout is used to display strings and other data types, while cout.write is used to display a specific number of characters from a string.

2. When should I use cout over cout.write in my C++ code?

Cout should be used when you want to display the entire string or data type. Cout.write should be used when you only want to display a specific number of characters from a string.

3. Can I use cout and cout.write interchangeably in my C++ code?

No, cout and cout.write have different functions and should be used according to their specific purposes. Using them interchangeably may result in unexpected outputs or errors in your code.

4. Is there a difference in performance between cout and cout.write in C++?

Yes, there is a slight difference in performance between the two. Cout is generally faster since it is a built-in function, while cout.write needs to go through additional steps to display a specific number of characters.

5. Can I use cout.write to display non-string data types in C++?

Yes, cout.write can be used to display non-string data types such as integers, characters, and floats. However, it will only display the raw binary data of these data types, so it is not recommended for regular output.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
964
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
769
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
Back
Top