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

  • Home
  • SEARCH
  • 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 8126625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:13:01+00:00 2026-06-06T07:13:01+00:00

In this C program #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main()

  • 0

In this C program

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
    int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU);

    dup2(stdout, file);
    system("ls -l");

    return 0;
}

I’m trying to redirect the output of system() to a file, for that i have used dup2 but it is not working.

What’s wrong with this code?
and, please tell me if there is any better way to do this? (without using > at the terminal )

  • 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-06T07:13:02+00:00Added an answer on June 6, 2026 at 7:13 am

    stdout is a FILE * pointer of the standard output stream. dup2 expects file descriptor, also you’ve messed up the parameters order.
    Use

    dup2(file, 1);
    

    instead.

    On the better-way-to-do-this part. This way is bad because you probably want to restore your standard output after this system call completes. You can do this in a variety of ways. You can dup it somewhere and then dup2 it back (and close the dupped one). I personally don’t like writing own cat implementations as suggested in other answers. If the only thing you want is redirecting a single shell command with system to a file in the filesystem, then probably the most direct and simple way is to construct the shell command to do this like

    system("ls -l > Result");
    

    But you have to be careful if filename (Result) comes from user input as user can supply something like 'Result; rm -rf /*' as the filename.

    Also, on the topic of security, you should consider specifying the full path to ls as suggested in the comments:

    system("/bin/ls -l > Result");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just to test, I ran this code #include<unistd.h> #include<stdio.h> #include<stdlib.h> int main() { int
My program is like this ( main.c ): #include <stdlib.h> #include <stdio.h> void main(){
Please have a look on this code: #include <unistd.h> #include <stdlib.h> #include <stdio.h> int
This is my code: #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> int num_mezzo_1(int
I make this program :: #include<stdio.h> char *raw_input(char *msg); main() { char *s; *s
I am studying pointers and I encountered this program: #include <stdio.h> void swap(int *,int
can any one give me the dry output for this program? #include <stdio.h> main()
I use the Frama-C tool to generate the dependence graph of this program(main.c). #include<stdio.h>
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>

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.