c++ - Why do spaces get lost in pipes in Linux? -
i have issue pipes in linux. looks space characters lost after piping. running 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 = 0; i<n && (cin >> c); i++) if(s[i] != c){ printf("wrong @ %d : '%c' != '%c' \n", i, s[i], c); break; } return 0; }
from
echo "ab cd" | ./checker
shell command gives
wrong @ 2 : ' ' != 'c'
is normal behavior? how avoid losing characters in pipes?
the problem isn't pipe, problem cin >> c
skips on whitespace.
it work if cin >> noskipws >> c
or this:
std::string q; getline(cin, q); for(i = 0; < n && < q.size(); i++) { if (q[i] != s[i]) ... }
Comments
Post a Comment