开发者

pipeline input data

I need to write a program that works with input from either a file or the shell (for pipeline processing). What is the most efficient w开发者_Python百科ay to deal with this? I essentially need to read the input line by line, but the input might be the output of another program from shell, or a file.

Thanks


I can't find the comments link, so post an answer. As Eugen Constantin Dinca said, pipe or redirect just output to the standard input, so what your program need to do is read from standard input.

I don't know what "read line by line" mean as you mentioned, something like ftp interactive mode? If that, there should be a loop in your program which read a line once a time and wait for the next input until you give the terminal signal.


Edit:

int c;
while(-1 != (c = getchar()))
    putchar(c);


Here's a C example from Echo All Palindromes, in C:

int main(int argc, char* argv[]) {
  int exit_code = NO_MATCH;
  if (argc == 1) // no input file; read stdin
    exit_code = palindromes(stdin);
  else {
    // process each input file
    FILE *fp = NULL;
    int ret = 0;
    int i;
    for (i = 1; i < argc; i++) { 
      if (strcmp(argv[i], "-") == 0)
        ret = palindromes(stdin);
      else if ((fp = fopen(argv[i], "r")) != NULL) {
        ret = palindromes(fp);
        fclose(fp);
      } else {
        fprintf(stderr, "%s: %s: could not open: %s\n",
                argv[0], argv[i], strerror(errno));
        exit_code = ERROR;
      }
      if (ret == ERROR) {
        fprintf(stderr, "%s: %s: error: %s\n",
                argv[0], argv[i], strerror(errno));
        exit_code = ERROR;
      } else if (ret == MATCH && exit_code != ERROR) 
        // return MATCH if at least one line is a MATCH, propogate error
        exit_code = MATCH;
    }
  }
  return exit_code;
}

To adapt it to C++: write function (it is palindromes above) that accepts std::istream&; pass it either std::cin (for standard input, or '-' filename) or ifstream objects from the main() function.

Use std::getline() with a given std::istream object inside the function to read input line by line (the function doesn't care whether input is from a file or stdin).


I think its a named pipe you want to work with. But from what I know the other program must write its output to the named pipe (If you have access to that program you can do that) and your program will read from the named pipe.

Hope this helps you.


I might be misinterpreting the question but I think you want your program to be able to be used like this: cat [some_file] | [your_program] or [your program] < [some_file].
If that's the case than you just need to read from the standard input (stdin/cin), the shell will take care of the rest.

If you want your program to either read from stdin or from a file you can do what a number of command line utils do, i.e. cat:

cat [OPTION] [FILE]...
...
With no FILE, or when FILE is -, read standard input.

For a code sample implementing the above see this article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜