I wrote a test program to test istream capabilities.
I wanted the program to accept both piped and terminal input.
However, cin fails after reading from the pipe.
For your information, I use Windows Vista with Watcom compiler.
Watcom doesn't have the library for input and output for strings.
The following is the program I wrote, named testpipe.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main(int argc,char*argv[])
{
int c=0;
char a[100];
string b;
while(cin.getline(a,100))
{
b=string(a);
if(c>=50){break;}
cout<<a<<"\n";
cout<<b.c_str()<<"\n";
c++;
}
cin.ignore(2,'\0');
cin.clear();
cout<<endl<<"Please enter something to test terminal input:"<<endl;
cin.getline(a,100);
b=a;
cout<<"RDstate function: "<<cin.rdstate()<<endl;
cout<<"Input:\t"<<a<<endl;
cout<<"Input(string):\t"<<b.c_str();
return 0;
}
|
and output1 from pipe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
C:\WATCOM\WORK>type testpipe.cpp|testpipe
#include<iostream>
#include<iostream>
#include<string>
#include<string>
#include<string.h>
#include<string.h>
using namespace std;
using namespace std;
int main(int argc,char*argv[])
int main(int argc,char*argv[])
{
{
int c=0;
int c=0;
char a[100];
char a[100];
string b;
string b;
while(cin.getline(a,100))
while(cin.getline(a,100))
{
{
b=string(a);
b=string(a);
if(c>=50){break;}
if(c>=50){break;}
cout<<a<<"\n";
cout<<a<<"\n";
cout<<b.c_str()<<"\n";
cout<<b.c_str()<<"\n";
c++;
c++;
}
}
cin.ignore(2,'\0');
cin.ignore(2,'\0');
cin.clear();
cin.clear();
cout<<endl<<"Please enter something to test terminal input:"<<endl;
cout<<endl<<"Please enter something to test terminal input:"<<endl;
cin.getline(a,100);
cin.getline(a,100);
b=a;
b=a;
cout<<"RDstate function: "<<cin.rdstate()<<endl;
cout<<"RDstate function: "<<cin.rdstate()<<endl;
cout<<"Input:\t"<<a<<endl;
cout<<"Input:\t"<<a<<endl;
cout<<"Input(string):\t"<<b.c_str();
cout<<"Input(string):\t"<<b.c_str();
return 0;
return 0;
}
}
Please enter something to test terminal input:
RDstate function: 6
Input:
Input(string):
|
note that it doesn't wait for input after accepting the piped content
output 2 from purely terminal input works fine
1 2 3 4 5 6 7 8 9 10 11
|
C:\WATCOM\WORK>testpipe
salkdfjl
salkdfjl
salkdfjl
^Z
Please enter something to test terminal input:
dfslkajlsdf
RDstate function: 0
Input: dfslkajlsdf
Input(string): dfslkajlsdf
|
Thanks for reading. I would like to point out that the cin.clear() does not work as I expect. Any suggestions to solve this problem is appreciated. I noticed that my post is kind of lengthy, and I hope not to see it quoted in replies below.