Advertisement
DimaT1

pipe to less

Apr 1st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. void write_data(FILE *stream) {
  6.     int i;
  7.  
  8.     for (i = 0; i < 100; ++i) {
  9.         fprintf(stream, "%d\n", i);
  10.     }
  11.  
  12.     if (ferror(stream)) {
  13.         fprintf(stderr, "Output to stream failed.\n");
  14.         exit(-1);
  15.     }
  16.  
  17.     fclose(stream);
  18. }
  19.  
  20. int main() {
  21.     FILE *output;
  22.     output = popen("less", "w");
  23.  
  24.     if (!output) {
  25.         fprintf(stderr, "Incorrect parameters or too many files.\n");
  26.         exit(-1);
  27.     }
  28.  
  29.     write_data(output);
  30.  
  31.     if (pclose(output) != 0) {
  32.         fprintf(stderr, "Could not run more or other error.\n");
  33.     }
  34.  
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement