Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:04:09+00:00 2026-06-06T15:04:09+00:00

Is it possible to read intermittently-sent data through a named pipe using redirection to

  • 0

Is it possible to read intermittently-sent data through a named pipe using redirection to stdin?

What I’d like to do is this:

$ mkfifo pipe
$ ./test < pipe

In another terminal:

$ cat datafile > pipe
$ cat datafile > pipe

repeating dumping information into the pipe. This only works the first time.

Here’s a demonstration program for test that shows the behavior:

int main(int argc, char *argv[]) {  
    char input_string[30];
    while(1) {
        while( cin.read(input_string, 30) || cin.gcount()!=0 ) {
            cout << "!" << endl;
        }
    }
return 1;
}

So, what’s going on? Does redirection only provide the contents of a single send to the pipe? I’ve already written a version of the actual production code that takes in the name of the pipe as a parameter and keeps it open for writing this way, and maybe that’s the answer. But I’m wondering if there’s a way to do this with redirection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:04:10+00:00Added an answer on June 6, 2026 at 3:04 pm

    When you redirect the input from the pipe like this:

    ./test < pipe
    

    The shell opens the pipe for reading and then starts your program. But opening the pipe does not complete until a writer exists — that is, open(2) blocks. When another process opens the pipe for writing, the original open call completes, and the two can communicate. When the writer closes its end of the pipe, the read end also closes — the reader gets an EOF.

    Once that cycle completes, you can reopen the pipe for reading and start another cycle, but you have to do it yourself. So if you’re reading for stdin, you’ll have to restart your program. Alternatively, you can just reopen the pipe on a different file descriptor, e.g.:

    // Error checking omitted for expository purposes
    int main(int argc, char **argv)
    {
        while(1)
        {
            int fd = open("pipe", O_RDONLY);
            char buffer[30];
            int n;
            while((n = read(fd, buffer, sizeof(buffer)) > 0)
            {
                // Process input
            }
            close(fd);
        }
    
        return 0;
    }
    

    If you want to wrap the raw I/O in a stdio FILE*, you can use fdopen(3); I’m not aware of a way to wrap a file descriptor in a C++ stream object, though it might be possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is possible to read from a txt file in loop like this? files names:
I know that it is possible read and write data from mongodb via hadoop.
Possible Duplicate: Read Post Data submitted to ASP.Net Form I have a google checkout
is it possible to read mongodb data with hadoop connector but save output as
Possible Duplicate: Read a file from line X to line Y? I know this
Is it possible to read a specific line using SuperCsv? Suppose a .csv file
Is it possible to read and write csv files using FileSystemObject in VBA?
Possible Duplicate: read/write to Windows Registry using Java I want to read the registry
Is it possible to read a line from a gzip-compressed text file using Python
Is it possible to read the data in the php $_SESSION array in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.