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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:15:14+00:00 2026-06-14T13:15:14+00:00

Using the getopt function included in unistd.h in C++, is there a way to

  • 0

Using the getopt function included in unistd.h in C++, is there a way to structure the optstring such that…

[-a] [-f "reg_expr"] out_file1 [[-f "reg_expr"] out_file2 ...] is possible?

This is a homework assignment, but the emphasis is not on this specific subtask.

In my head I would like to specify the following logic:

(a argument), (infinitely many f arguments with 2 required (sub)arguments),… (infinitely many generic arguments)

Perhaps my understanding of the getopt function is fundamentally flawed. I also saw a getopt_long. Perhaps that is what I’m missing.

I originally drafted this, which worked, but I came across the getopt function and thought it might do a better job.

int outFileFlags;
int outFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
int i = 1;
while (i < argc){
    if (i == 1 && strcmp( argv[i], "-a") == 0){
        cout << "append flag set" << endl;
        outFileFlags = O_RDWR | O_APPEND;
        i++;
        continue;
    }
    else {
        outFileFlags = O_TRUNC | O_RDWR | O_CREAT;
    }
    if (strcmp( argv[i], "-f") == 0 && i+2 <= argc){
        cout << "   regx = " << argv[i+1] << endl;
        cout << "   fn = " << argv[i+2] << endl;
        i = i+3;
        continue;
    }
    else {
        cout << "   regx = none" << endl;
        cout << "   fn = " << argv[i] << endl;
        i++;
        continue;
    }
}

Note: assume this is written for a unix environment. I don’t think I can use anything from the standard library. I only included std::cout for testing purposes.

I will be happy to elaborate on any details of the assignment. However, the main question revolves around the syntax of the optstring. I am currently only aware of : meaning required and :: meaning optional is there a way to specify arguments that repeat like a regex wildcard *?

EDIT:

I’m sure this is sloppy due to the fact that I don’t think getopt is designed to handle multiple arguments per option but it does the trick…

int main(int argc, char *argv[]){
    char c;
    int iterations = 0;
    while (*argv) {
        optind = 1;
        if (iterations == 0){
            opterr = 0;
            c = getopt(argc, argv, "a");
            if(c == 'a'){
                //~ APPEND SET
            }
            else if(c=='?'){
                optind--;
            }
        }
        while ((c = getopt(argc, argv, "f:")) != -1) {
            if (c == 'f'){
                //~ REGEX = optarg
                if (optind < argc && strcmp(argv[optind], "-f") != 0) {
                    //~ FILENAME = argv[optind]
                    optind++;
                }
                else {
                    errno = 22;
                    perror("Error");
                    exit(errno);
                }
            }
            else {
                errno = 22;
                perror("Error");
                exit(errno);
            }
        }
        argc -= optind;
        argv += optind;
        iterations++;
        //~ REMAINING FILES = *argv
    }
}
  • 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-14T13:15:15+00:00Added an answer on June 14, 2026 at 1:15 pm

    You need to do a separate getopt loop for each group of options and output filename.

    group_index = 0;
    while (*argv) {
      optreset = 1;
      optind = 1;
      while ((ch = getopt(argc, argv, "af:")) != -1) {
        switch (ch) {
          /* process options */
        }
      }
      argc -= optind;
      argv += optind;
      outfile[group_index++] = *argv;
      argc--;
      argv++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking up the getopt command and I discovered that using the function
I'm using Perl's Getopt::Long module to parse command line arguments. However, it seems that
Could someone provide a simple example using getopt.pas with short and long command line
I am using the Getopt::Long module to process command line arguments. The typical behavior
Using SQL Server 2008 R2 we are looking for a way to select the
Using import datetime in python, is it possible to take a formatted time/date string
I'm using getopt to parse options and arguments. I wrote a batch file to
I have written a simple code using getopt for understanding perspective. #include <stdio.h> #include
I'm quite fond of using GNU getopt , when programming under Linux . I
I've written a wrapper program for mailx using perl that allows me to easily

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.