CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    1

    send file to program using following syntax "cat file.rle | ./code"

    Does anyone know how I would set up my program to allow a text file to be piped into my compiled code?

    it should work by issuing a command similar to the one below
    cat file.rle | ./code

    thanks

  2. #2
    Join Date
    Jan 2003
    Posts
    615

    Re: send file to program using following syntax "cat file.rle | ./code"

    Sorry, this does not make any sense to me. Please explain with a bit more detail. What do you hope to accomplish?
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  3. #3
    Join Date
    Apr 2009
    Posts
    598

    Re: send file to program using following syntax "cat file.rle | ./code"

    Use stdin in your program to get the data passed to the pipe.
    Code:
      c = (char) fgetc(stdin);
      printf("%c", c);
    stdin is a keyword recognized by your C compiler.
    Use fgetc() or fread(). Don't use fopen() and fclose().

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: send file to program using following syntax "cat file.rle | ./code"

    Quote Originally Posted by dustout View Post
    it should work by issuing a command similar to the one below
    cat file.rle | ./code
    Your program just needs to read from standard input. The shell command sends the content of the file as input for your program.

    Thus the following program:
    Code:
    #include <iostream>
    #include <string>
    
    int main() {
      std::string word;
      std::cin >> word;
      std::cout << "Input starts with the word " << word << "\n";
    }
    will print the first word of the file.rle that you pipe to it.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured