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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:32:49+00:00 2026-06-18T21:32:49+00:00

This is a homework assignment first off. We have to create a common application-programming

  • 0

This is a homework assignment first off.

We have to create a “common application-programming model in UNIX/Linux known as the filter”.

I’m stuck on reading the input passed through as arguments (it’s all I ever seem to have trouble on).

For example, the cmd is open and the following line is entered:

program -isomebinaryfile.bin

I need to determine what the first letter is after the hyphen (-) and so on and so forth.

Is scanf what I would be using? My main is set up to be able to accept arguments:

int main (int argc, char *argv[])
{
FILE *inf = NULL;
char *arg = argv[0];
}

Can someone give me a little help?

  • 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-18T21:32:50+00:00Added an answer on June 18, 2026 at 9:32 pm

    Unless your assignment is only to handle processing of arguments, you may want to look up getopt – it’s a standard library parser for arguments.

    As for the meat of your question, there are a lot of options, and you could use sscanf as part of it, but you don’t have to.

    To parse the one argument you mentioned, you need to do the following: check if the argument begins with -i, grab the data out of the argument.

    The easiest way to check if the argument begins with -i is:

    if (argv[1][0] == '-' && argv[1][1] == 'i')
    

    Alternatively, if you have a lot of argument options, all beginning with ‘-‘, you may want something like:

    char * i = NULL;
    char * o = NULL;
    char * s = NULL;
    for (int i = 1; i < argc; ++i) {
        if (argv[i][0] == '-') {
            switch(argv[i][1]) {
                case 'i':
                    i = argv[i][2];
                    break;
                case 's':
                    s = argv[i][2];
                    break;
                case 'o':
                    o = argv[i][2];
                    break;
                default:
                    cerr << "Unknown option: " << argv[i][1];
            }
         } else {
            cerr << "Error: all options must begin with '-'";
         }
    

    Note, I’m using argv[1], not 0. argv[0] is always the name of the executable.

    The fastest way to extract the rest of the argument is simple pointer arithmetic:

    char * filename = argv[1] + 2;  // (Or you could equivalently say = &argv[1][2]
    

    This is most efficient – it reuses the strings that are already in argv. If you’re planning on changing the strings around, you’d do better with strcpy:

    char * filename = (char *)malloc(strlen(argv[1]) - 2);
    strcpy(filename, argv1 + 2);
    // and eventually you'd have to free(filename)...
    

    Play around and experiment with all the string functions. You’ll find them essential to all of your later programs.

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

Sidebar

Related Questions

So first off, this is a homework assignment, so please don't write any code
This is my first question, and yes, it is a homework assignment. I have
First of all, this is a homework assignment. I'm trying to make some bubbleSort
(this is indirectly a part of a much larger homework assignment) I have something
I have a homework assignment to sort an array in ascending order. Obviously, this
First off this is for homework or... project. I'm having trouble understanding the idea
This is a homework assignment and my first experience using RegEx. I am starting
Disclaimer: this question is directly related to my programming homework. My C++ assignment consists
I have to write this code for a homework assignment but I don't even
First, I would like to say that this is for a homework assignment and

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.