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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:03:23+00:00 2026-06-03T09:03:23+00:00

I have a C++ program which has the prototype of the main function as

  • 0

I have a C++ program which has the prototype of the main function as follows:

int main(int argc, char * argv[])

The code hasn’t been written by me, but this is a single C file available here.
When I compile this code, and through the command line run it as:

someexe in.txt > out.txt
This gives me an output out.txt which is generated in the same directory by operating on some input from in.txt.

someexe in.txt out.txt
This gives me an output on the command line itself. (without using > operator)

However, instead of passing the command line argument and without using the output redirection > operator, I have been trying to call the main function from another function and passing the parameters myself. If I pass an array of char* {fileDirName, in.txt}, I am not sure how to go about generating an out.txt (since I think > output redirection is an operating system level function available in command line).

Any suggestions would be greatly appreciated

The program in the link is readily available as copy paste and can be tried (main function is written at the last in the above 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-03T09:03:24+00:00Added an answer on June 3, 2026 at 9:03 am

    Assuming the aim is to mimic the output redirection feature (> out.txt) of the shell you can do something like:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <assert.h>
    #include <unistd.h>
    #include <iostream>
    
    int main() {
      int fd = open("out.txt", O_WRONLY|O_CREAT|O_TRUNC, 0660);
      assert(fd >= 0);
      const int ret = dup2(fd, 1);  
      assert(ret >= 0);
      std::cout << "Hello redirected world!" << std::endl;
      close(fd);
    }
    

    You can do similar for stdin also, to mimic the input redirection (< in.txt). These will be preserved across calls to exec() too.

    Of course it would be simpler to modify the program to write to the place you wanted given you have the source available.

    Note though that dup2(), which “swap” the stdout fd for the one we just opened is non-portable. IIRC open() (as opposed to fopen()) is UNIX specific also)

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

Sidebar

Related Questions

I currently have a program which has the following basic structure main function --
I have a C program which has a function call that is defined in
I have the .exe of a program which has been generated from C++. Is
I have a C program which has multiple worker threads. There is a main
I have a program which has a text box that the user can edit.
I have a C program which has always used hard coded define statements for
I have a .NET 2.0 program that has a reference to Interop.WIA.dll (.NET), which
The whole story: I have created a fancy .NET program which has an installer
Suppose I have have a program P which has a filename as argument. For
I have a main program which creates a collection of N child threads to

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.