0

I have an issue with pipes in Linux. Looks like space characters are lost after piping. Running the following C++ code

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(){
    char s[] = "ab cd", c;
    int n = strlen(s);
    for(int i = 0; i<n && (cin >> c); i++)
        if(s[i] != c){
            printf("wrong at %d : '%c' != '%c' \n", i, s[i], c);
            break;
        }
    return 0;
}

from

echo "ab cd" | ./checker

shell command gives

wrong at 2 : ' ' != 'c'

Is it normal behavior? How to avoid losing characters in pipes?

Jay Foreman
  • 600
  • 1
  • 6
  • 17
  • 1
    Because CIN consumes whitespace. http://stackoverflow.com/questions/11462021/issue-with-cin-when-spaces-are-inputted-using-string-class – kfsone Aug 23 '13 at 22:39
  • 1
    @kfsone - `cin` doesn't consume whitespace; stream extractors (`operator>>`) do, regardless of what stream the data is coming from. – Pete Becker Aug 24 '13 at 12:16
  • @PeteBecker Yep, bad wording on my part. – kfsone Aug 24 '13 at 18:03

2 Answers2

5

The problem isn't the pipe, the problem is cin >> c which skips over whitespace.

It would work if you do cin >> noskipws >> c

or something like this:

std::string q;
getline(cin, q);

for(i = 0; i < n && i < q.size(); i++)
{
  if (q[i] != s[i]) 
    ...
}
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

This is the default behaviour when using cin and has nothing to do with pipes.

You may want to tell the stream not to ignore white spaces:

std::cin >> std::noskipws;

Then proceed as you're already doing.

syam
  • 14,701
  • 3
  • 41
  • 65
  • 1
    That is the default behavior when using stream extractors (`operator>>`), regardless of what stream the extractor is applied to. In addition to using `std::noskipws`, you can read individual characters without skipping whitespace with `basic_istream::get()`. `operator>>` is a **formatted** stream operation; `get` is unformatted. – Pete Becker Aug 24 '13 at 12:17