We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


simple cin/cout ?

cmain asked
Medium Priority
Last Modified: 2008-02-01
I would like to read stdin until there is no more stdin, and then dump it to stdout.
Catch is: I must be able to do this with binary and ascii files, and I need to implement it with the stl.

(If this cannot be done with binaries - please let me know why. I have a feeling that because stdin is a text stream, that it won't work).

I am writing a pipe application.
I need to feed the pipe data to an out of process OLE server eventually.

Watch Question

#include <stdio.h>

int main(void)
   char data;
   return (0);


I asked for an implementation using STL.
(Standard Template Library).


I would also like to know the number of bytes that were piped into the application??

Why use the STL?  Why not just use <iostream>?  
#include <iostream>
int main()
  unsigned char ch;
  unsigned long count;
  while ( (cin >> ch).good() )
    cout << ch;
    // optionally:-
    cerr << "Transferred " << count << " bytes\n";
    return 0;


That works fine,
there is just one problem.

It stops at the first eof / non printable character.
I need to be able to pipe binary data. I want to use C++ io.
Your suggestion is fine except for the binary data problem.

I believe that a stream can be switched between binary and text mode.  But I can't see how at the moment.


I have here a sample program.
This actually works, sort of.
Call this application MYAPP

I can do the following.

cat MYFILE | MYAPP > NewFile.

The problem is that MYFILE is approximately 1300 bytes bigger than the resultant NewFile.

I know for a fact that my cat program works correctly.
I don't have source but...
cat MYFILE | cat > NewFile
results in an identical copy of MYFILE in NewFile,
which is what I want.

I am really getting confused here. I am trying to figure out what character is making the darn thing stop prematurely.
I have tried flushing cout with cout.flush(), but that doesn't help.

the two _setmode lines effectively change the streams to binary mode. I know this because otherwise the program stops after 78 bytes of a 65K file. With the _setmode I get about 64K of the 65K.

I don't really care if my final solution uses STL <iostream> or  the standard C++ libraries <iostream.h>, but I don't want to use getc() unless I really have to (call me difficult if you want!)

#include <malloc.h>
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <wtypes.h>

void main ()
      using namespace std;
      TCHAR chr;
      _setmode( _fileno( stdin ), _O_BINARY );
      _setmode( _fileno( stdout ), _O_BINARY );
      while( (cin>>chr).good() )
            cout << chr;
      cerr << "done";
Unlock this solution and get a sample of our free trial.
(No credit card required)


Hey hey,
Thanks very much.

I was wondering if you perhaps know why the extraction operators cannot be used? read and write do work perfectly.


My personal opinion is that the operators are provided just for convenience.

Can't it be done with the STL copy() function and iostream iterators?  That would look cleaner.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.