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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:19:03+00:00 2026-06-15T15:19:03+00:00

Quick Edit: This is a homework assignment. My goal is to take in a

  • 0

Quick Edit: This is a homework assignment. My goal is to take in a few cl arguments for my program (either -s, -w with a width length, and the file) and word wrap the file that according the default length of 40 characters or a new number if the user chooses the ‘-w’ option.

I’m trying to write a C program that takes in the arguments via command prompt (the executable is named “wrapfile.exe”). The program isn’t done and more is to be added, this is just a part of it that is causing me mayhem.

Here would be an example of valid command prompt entries:

C:\"within wrapfile.exe's directory"> wrapfile -s filename.txt
C:\"within wrapfile.exe's directory"> wrapfile -w 5 filename.txt
C:\"within wrapfile.exe's directory"> wrapfile -s -w 50 filename.txt

etc.

Example of invalid entries:

C:\"within wrapfile.exe's directory"> wrapfile 
C:\"within wrapfile.exe's directory"> wrapfile -w
C:\"within wrapfile.exe's directory"> wrapfile qwer

etc.

My issue is it cannot detect the number after I enter “-w” ..

Here is the code:

#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "string.h"


int main(int argc, char *argv[])
{
int output = 0;
int commands = 1;
int wraplength= 41;
int i=0;
int counter=0;
int wordwrap = 0;
int ExitStatus = 1;
int input = 1;
int w = 0;
int s = 0;
FILE *f = NULL;


for (i=0; i < argc; i++)
{
    if ( (*argv[input] + i-1) == '-') // check for option
    {
        printf(" - detected first");
        if (*(argv[input] + i  ) == 's') // check for wordwrap
        {
            printf(" s detected");
            i++;
            i++;
            s = 1; // set s to true to that option can be added later
            wordwrap = 1; // set wordwrap on or true
        }   

        if (*(argv[input] + i) ==  'w')//if the second option is a w
        {
            i++;
            printf(" w detected ");
            sscanf ((argv[input] + i), "%d", &wraplength);
            printf ("%d", wraplength);
            if ( wraplength < 1) // check what the number is
                {
                    printf("Usage: wrapfile [-s] [-w width] file ...");
                    return 2; // command line options incorrect
                }               
        }

        if (*(argv[input] + i) == '-')
        {
            printf(" second - detected");
            i++;

            if (*(argv[input]+ i) ==  'w')//if the second option is a w
            {
                i++;

                if (sscanf ((argv[(input)+1]), "%d", &wraplength) != 1) // check what the number is
                {
                    printf("Usage: wrapfile [-s] [-w width] file ...");
                    return 2; // command line options incorrect
                }
            }
        }
    }
}
return 0;
}

BIG EDIT:
I took Dietrich Epp suggestion and here is something I did with it. It seems my program crashes every time I try to check an argument after “-s”. How can I check the next arguments (if there is none?) without crashing my program. I know that this line has something to do with the crashing:

   arg = argv[i++];

Here’s the code:

while (i < argc) 
{
    arg = argv[i++];
    if (!strcmp(arg, "-s")) 
    {
        arg = argv[i++];
        son = 1; 
        printf("Have -s\n");
        if (!strcmp(arg, "-w"))
        {
            if (i >= argc)
            {
                printf("Usage: wrapfile [-s] [-w width] file ...");
            }
            param = argv[i++];
            wraplength = *param;
            printf("Have -w %s\n", param);
        }

    }
  • 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-15T15:19:05+00:00Added an answer on June 15, 2026 at 3:19 pm

    I think you’re mixing up your loop variables here.

    This makes i loop over all arguments, including argv[0] which you usually don’t want.

    for (i=0; i < argc; i++)
    

    This uses i as an index into one of the argument strings, but with funny syntax.

    if (*(argv[input] + i  ) == 's')
    

    On other systems you’d just use getopt(), but that’s not always an option on Windows.

    Suggestion

    You’ll want a loop more like this:

    // Note: C99, so you will need to translate to C89 if you use Microsoft's
    // C compiler
    int i = 1;
    while (i < argc) {
        char *arg = argv[i++];
        if (!strcmp(arg, "-s")) {
            printf("Have -s\n");
        } else if (!strcmp(arg, "-w")) {
            if (i >= argc)
                error();
            char *param = argv[i++];
            printf("Have -w %s\n", param);
        } else {
            error();
        }
    }
    

    Command option parsing is so incredibly not relevant to your program’s performance that the above chain of if/else blocks and strcmp() are fine.

    Warning!

    You will not be able to specify arbitrary filenames with this! If you get the arguments from main(), they will be converted to whichever code page you are currently using, which is horribly broken for almost any purpose. (It might be fine if you are the only one using the program.)

    In order to specify arbitrary filenames, you will need to call GetCommandLineW() to get the command line in UTF-16, and then CommandLineToArgvW() to parse it to int argc and wchar_t **argv.

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

Sidebar

Related Questions

Does anyone know how to enable or disable programmatically the Quick Edit Mode in
I have a table with a quick edit function (updates only one field using
I have a feeling this will be a quick fix, given that I started
Edit: This may be a 5.0-specific bug. (I'm on 5.0.83). Removing the innodb_log_file_size setting
Just doing a quick edit up at the top of my post. The question
EDIT: This question is now resolved. I managed to create exactly what I needed,
Quick question. Is it okay to save a PHP class file as myClass.class and
I have a console program that has different components that run like this: void
edit : I based this question on a false assumption - that the generic
I have a quick edit jquery dialog that allows my users to change some

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.