Pipe in a string to a program with special characters

Hi
I need some help >< I finally figured out how to read in a pipeline from say echo. However I toss in a few newlines and it does to garbage!
1
2
3
string in;
if(cin)
cin >> in;


give it say echo "foo" | a.out
we are good
Now give it
echo -e "fo\no" | a.out
I get just -e

I tried to fix it
1
2
3
4
5
while(cin){
cin >> temp;
userin+=temp;

}


and now I get
echo -e "ab\ncd" | a.out
-eabcdcd. Was read
Can you post the whole code snippet ? I believe \n is interpreted as the line terminator and cin will stop after that and that is why you see garbage.

If you want \n to be a valid input data, then you may want to set your delimiter to other characters besides \n.
hah yeah that really was the contents but here you go =)
It's just a test method whire I figure it out for my real program. I can't post the contents of that application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <iostream>
#include <vector>
#include <unistd.h>
#include<fstream>
using namespace std;

int main(int argc, char **argv) {
string userin;
string temp;
while(cin){
cin >> temp;
userin+=temp;

}
cout << userin << ". Was read"<<endl;

}


before it was

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <iostream>
#include <vector>
#include <unistd.h>
#include<fstream>
using namespace std;

int main(int argc, char **argv) {
string userin;
string temp;
if(cin){
cin >> temp;
userin+=temp;

}
cout << userin << ". Was read"<<endl;

}


Also I noted that it waits till something is read... that really isn't what I want it to do...

Essentially I either need to provide a fline to be read, pipe a string in or command line a string in. Only one of 3 (I guess I can do a if(argc==1) then try to pipe it...)

really it should be going with the echo -e to handle the new lines. the -e option says to process it. Instead it's doing this odd stuff...
Last edited on
Seem the other topic on feof is haunting us again. For your observation change to below code.

1
2
3
4
while(cin >> temp){
//cin >> temp;
userin+=temp;
}


And he should work as expected even when you echo -e "ab\ncd" | a.out

Topic archived. No new replies allowed.