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);
}
}
I think you’re mixing up your loop variables here.
This makes
iloop over all arguments, includingargv[0]which you usually don’t want.This uses
ias an index into one of the argument strings, but with funny syntax.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:
Command option parsing is so incredibly not relevant to your program’s performance that the above chain of
if/elseblocks andstrcmp()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 thenCommandLineToArgvW()to parse it toint argcandwchar_t **argv.