Difference between cout and cout.write in C++

  • Context: Comp Sci 
  • Thread starter Thread starter Ilikecereal
  • Start date Start date
  • Tags Tags
    C++ Difference
Click For Summary
SUMMARY

The discussion clarifies the differences between the C++ output stream object `cout` and its member function `cout.write()`. While both can output strings, `cout` utilizes the overloaded `<<` operator, allowing for a variety of data types to be streamed, whereas `cout.write()` is specifically designed for writing raw character arrays and requires explicit length parameters. The example provided demonstrates that `cout.write()` is more suitable for low-level string manipulation, while `cout` is more versatile for general output tasks.

PREREQUISITES
  • Understanding of C++ output streams
  • Familiarity with the `<<` operator overloading
  • Knowledge of character arrays and string manipulation in C++
  • Basic experience with the C++ Standard Library
NEXT STEPS
  • Explore C++ output stream manipulation techniques
  • Learn about operator overloading in C++
  • Investigate the performance implications of using `cout` vs. `cout.write()`
  • Study character arrays and their handling in C++
USEFUL FOR

C++ developers, software engineers, and students learning about output streams and string handling in C++.

Ilikecereal
Messages
17
Reaction score
1
What's the purpose of count.write. Isn't it pretty much the same thing as count?

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

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

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

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 24 ·
Replies
24
Views
2K
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K