Solved

simple cin/cout ?

Posted on 1998-09-10
12
549 Views
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.

Regards
-craig.
0
Comment
Question by:cmain
  • 5
  • 2
  • 2
  • +3
12 Comments
 
LVL 3

Expert Comment

by:gcauthon
ID: 1172429
#include <stdio.h>

int main(void)
{
   char data;
   while(fread(&data,1,1,stdin)==1)
   {
      printf("%c",data);
   }
   return (0);
}
0
 
LVL 1

Author Comment

by:cmain
ID: 1172430
I asked for an implementation using STL.
(Standard Template Library).

0
 
LVL 1

Author Comment

by:cmain
ID: 1172431
I would also like to know the number of bytes that were piped into the application??

Thanks
-craig.
0
 
LVL 2

Expert Comment

by:JYoungman
ID: 1172432
Why use the STL?  Why not just use <iostream>?  
0
 
LVL 2

Expert Comment

by:JYoungman
ID: 1172433
#include <iostream>
int main()
{
  unsigned char ch;
  unsigned long count;
  while ( (cin >> ch).good() )
    {
    ++count;
    cout << ch;
    }
    // optionally:-
    cerr << "Transferred " << count << " bytes\n";
    return 0;
}

0
 
LVL 1

Author Comment

by:cmain
ID: 1172434
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:nietod
ID: 1172435
I believe that a stream can be switched between binary and text mode.  But I can't see how at the moment.
0
 
LVL 1

Author Comment

by:cmain
ID: 1172436
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;
      cout.flush();
      cerr << "done";
}
0
 
LVL 1

Accepted Solution

by:
Bonev earned 40 total points
ID: 1172437
Try this:
#include <malloc.h>
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <wtypes.h>

void main ()
{
      using namespace std;
      _setmode( _fileno( stdin ), _O_BINARY );
      _setmode( _fileno( stdout ), _O_BINARY );
      int rd = 1;
      char buf[128];
      while( rd )
      {
            cin.read(&buf[0], sizeof(buf));
            rd = cin.gcount();
            cout.write(&buf[0], rd);
      }
      cout.flush();
      cerr << "done";
}
0
 
LVL 1

Author Comment

by:cmain
ID: 1172438
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.

Thanks.
Regards
-craig.

0
 
LVL 1

Expert Comment

by:Bonev
ID: 1172439
My personal opinion is that the operators are provided just for convenience.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1172440
Can't it be done with the STL copy() function and iostream iterators?  That would look cleaner.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now