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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:13:44+00:00 2026-05-27T13:13:44+00:00

I want the put a sub-option in a string so that I can use

  • 0

I want the put a sub-option in a string so that I can use it as a file name for reading a file:

char *nvalue = NULL;
char *dvalue = NULL;
char *input = NULL;
char inputfilename[] = "\"";
int ar;

int main(int argc, char *argv[])
{
   while ((ar = getopt(argc, argv, "hn:d:i:")) != -1)
      switch (ar)
      {
         case 'h':
            printf("something");
            break; /* added */
         case 'n':
            nvalue = optarg;
            if (isdigit(nvalue))
               stop = atoi(nvalue);
            else
               printf("something\n");
            break; /* added */
         case 'd':
            dvalue = optarg;
            if (!strcmp(dvalue, "FCFS")   || !strcmp(dvalue, "SSTF") ||
                !strcmp(dvalue, "C-SCAN") || !strcmp(dvalue, "LOOK"))
               ;
            else
               printf("Invalid type of disk scheduling policy entered.\n");
            break; /* added */
         case 'i':
            input = optarg;
            strcpy(inputfilename, optarg);
            printf("Filename :%s\n", inputfilename);
            break;
      }
   /* ... */
}

So on the command line if I input:

./foobar -i hello

then I should be able to read the file with:

FILE *file = fopen(inputfilename, "r" );

any suggestions? answers?
thanks!

  • 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-27T13:13:45+00:00Added an answer on May 27, 2026 at 1:13 pm

    There are a number of problems with your code. I’m ignoring the absence of header files (assuming your code uses the correct ones so all functions have a prototype in scope before use). I’m also reformatting your code ruthlessly but without further comment.

    char *nvalue = NULL;
    char *dvalue = NULL;
    char *input = NULL;
    char inputfilename[] = "\"";
    

    This allocated an array of two bytes as inputfilename. I hate to think what’s going to happen when you use it.

    int ar;
    

    There is every reason why this variable should be local to the main function and no reason visible for it to be a global variable. Unless you have a header declaring them, the other variables should also be static – assuming that you need to access their values outside of main() without a convenient way to pass them as locals. Avoid global variables whenever possible.

    int main(int argc, char *argv[])
    {
        while ((ar = getopt(argc, argv, "hn:d:i:")) != -1)
        {
            switch (ar)
            {
            case 'h':
                printf("something");
    

    Oops; no break, so the code drops through to the case 'n': code. C is not Pascal.

            case 'n':
                nvalue = optarg;
                if (isdigit(nvalue))
                    stop = atoi(nvalue);
    

    You haven’t shown a declaration for stop. Unless you really need the string, you can do without nvalue, avoiding a global variable, which is always desirable.

                else
                    printf("something\n");
    

    Another missing break; I’m not going to point it out again.

            case 'd':
                dvalue = optarg;
                if (strcmp(dvalue, "FCFS")   == 0 ||
                    strcmp(dvalue, "SSTF")   == 0 ||
                    strcmp(dvalue, "C-SCAN") == 0 ||
                    strcmp(dvalue, "LOOK"    == 0)
                {
    

    I’d suggest a comment such as /* Nothing - dvalue is OK */. Or inverting the condition using De Morgan’s theorem:

                if (strcmp(dvalue, "FCFS")   != 0 &&
                    strcmp(dvalue, "SSTF")   != 0 &&
                    strcmp(dvalue, "C-SCAN") != 0 &&
                    strcmp(dvalue, "LOOK"    != 0)
    

    You might even decide to encapsulate that test into a function which tests the value against each element of an array of codes.

                }
                else
                    printf("Invalid type of disk scheduling policy entered.\n");
    

    It would be courteous to provide a list of the acceptable values – which suddenly becomes another reason for having an array of valid values which you can use to generate the list. Very often, error messages should be reported on stderr (using fprintf()) rather than stdout.

            case 'i':
                input = optarg;
    

    This assignment is sufficient.

                strcpy(inputfilename, optarg);
    

    Unless the user types a single-character file name, you’ve just overflowed the inputfilename array. You really didn’t need to make a copy of the argument unless you’re going to modify the name (for example, to add or change the extension on the name).

                 //strcat(inputfilename,"\"");   
                 printf("Filename :%s\n", inputfilename);
            }
    

    You didn’t include a default clause. Since ar will be assigned the value ? when the user-provided option is not recognized, this is normally your cue to provide a simple usage message and exit.

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

Sidebar

Related Questions

hey, hi i want put limit on object creation means a class can have
I have a page layout for my MOSS '07 site that I want put
When I want to use a branch in CC, I usually put something like
I have a basic database that I want to pull out sub data (related)
I've projected an Intranet Ajax application and I want put it in a full
I want to put a copyright notice in the footer of a web site,
I want to put random output from my result set (about 1.5 mil rows)
I want to put my dependent files in the app directory. I seem to
I want to put some common information in my MasterPage to be shown on
I want to put individual JComboBoxes into each cells of a JTable. ie. The

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.