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 7985657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:37:14+00:00 2026-06-04T11:37:14+00:00

I am trying read stdout of my own program into 2 arrays like this

  • 0

I am trying read stdout of my own program into 2 arrays like this

#include<stdio.h>

int main ()
{
    char arr[100]={0};
    char arr2[100]={0};

    printf("Hello world\n"); // This writes to stdout

    fgets( arr, 80, stdout );

    fseek ( stdout, 0, SEEK_SET );

    fgets ( arr2, 80, stdout );
    printf ("First array is %s\n", arr );
    printf ("Second array is %s\n", arr2 );

    return 0;

}

The output is not what I expect. That is both the arrays are empty instead of containing Hello World as I expected.

I went through this post which suggests dealing with pipes to accomplish what I want but doesn’t tell me why my above code doesn’t work?

EDIT: Though it would be nice to know alternatives to make the above work as it should, I am more curious on the problems involved in reading stdout of the same program

  • 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-04T11:37:15+00:00Added an answer on June 4, 2026 at 11:37 am

    Not every file is seekable, readable or writeable. Stdout is usually a kind that can’t be read back.

    Most likely, stdout will be a pipe. In that case, your program holds the writable end, and someone else holds the readable end. The pipe implementation just transfers data and does not keep it; once it has been read at the other end, there is no way to get it back.

    If you want a file that can be read back, create a regular temporary file, or your own pipe, and use fprintf/fscanf instead of printf/scanf. Alternatively, do freopen on stdout to reassign it to another file/pipe, then printf will operate on that new file.

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

Sidebar

Related Questions

Here is my code: #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> int main(int argc,char
I am trying to read a binary file from a program that writes a
During the runtime of a process I would like to read its stdout and
I'm trying to run a process and when it's done, read stderr and stdout.
I'm Trying to read from a socket and print to stdout using printf (a
Trying to read an RSS and select information using Linq but can't seem to
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =
Im trying to read a column of String values from my DB. Im trying
Iam trying to read a binary file to memory and pass the starting address
I'm trying to read the event log for a security audit for all users

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.