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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:39:55+00:00 2026-05-29T14:39:55+00:00

Newbie C++ programmer here. I’m trying to write a command line application that takes

  • 0

Newbie C++ programmer here. I’m trying to write a command line application that takes two arguments, an input file and an output file. However, if the input file or the output file name is “-“, I need the program to read/output to standard input/output instead. My problem is that in C++, I don’t know how to do this without the compiler not knowing that the input/output streams are initialized. Here’s the code I have.

if(argv[1] == "-") {
  istream input;
  cout << "Using standard input" << endl;
}
else {
  ifstream input;
  cout << "Using input file " << argv[1] << endl;
  input.open(argv[1], ios::in);
  if(!input) {
    cerr << "Cannot open input file '" << argv[1]
    << "', it either does not exist or is not readable." << endl;
    return 0;
  }
}

if(argv[2] == "-") {
  ostream output;
  cout << "Using standard output" << endl;
}
else {
  ofstream output;
  cout << "Using output file " << argv[2] << endl;
  output.open(argv[2], ios::out);

  if(!output) {
    cerr << "Cannot open output file '" << argv[2] << "' for writing."
    << " Is it read only?" << endl;
    return 0;
  }
}

From here I cannot call the operator >> on input, because, I’m guessing, the compiler doesn’t know it’s been initialized.

  • 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-05-29T14:39:56+00:00Added an answer on May 29, 2026 at 2:39 pm

    You can use a reference to a stream, and then initialize it to refer to either a file stream or standard input or output. The initialization has to happen in a single command, though, so you’ll have to declare the file streams even if you don’t use them.

    ifstream file_input;
    istream& input = (strcmp(argv[1], "-") == 0) ? cin : file_input;
    
    ofstream file_output;
    ostream& output = (strcmp(argv[2], "-") == 0) ? cout : file_output;
    

    Notice the & in the declarations of input and output. They indicate that we’re not declaring a separate stream object, but rather just declaring a reference to some other stream object, which we select conditionally based on the value of argv[x].

    Then you can open the files, if you need to. The drawback is that we need to check for “-” strings twice instead of just once for each input or output.

    if (strcmp(argv[1], "-") == 0) {
      cout << "Using standard input" << endl;
    } else {
      cout << "Using input file " << argv[1] << endl;
      file_input.open(argv[1]);
      if (!file_input) {
        cerr << "Cannot open input file '" << argv[1]
             << "', it either does not exist or is not readable." << endl;
        return 1;
      }
    }
    

    Thereafter, you can read from input and write to output, and the files or the standard I/O streams will be used.


    Note the other changes I made to your code. First, I call strcmp instead of using the == operator; the operator doesn’t do what you think it does when comparing a char* with a literal. Next, when opening the file fails, I return 1 instead of 0. Zero indicates a successful program, whereas non-zero tells the OS that the program failed.

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

Sidebar

Related Questions

Newbie programmer here trying to implement quicksort, yet it won't work. I've looked at
Newbie to LINQ, and trying to write the following query... select f.Section_ID, f.Page_ID, f.SortOrder,
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
I'm a newbie programmer doing his best to self learn PHP. I'm trying to
newbie programmer here. I have 3 tables namely product, category, and subcategory. I configured
I'm a newbie programmer trying to jump in to Python by building a script
i am a newbie programmer in ASP.NET. So My database have two tables: UserTypes
newbie programmer and lurker here, hoping for some sensible advice. :) Using a combination
I'm a newbie programmer trying to build a demo PHP site on my computer
Newbie to C++ but I am a Java programmer. Trying to learn C++ now.

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.