-1

I have a function with width currently defined as "unsigned long width" and then define it as "width = 16".

Instead, I'd like 16 to be the default number, but I want there to be an option to set a different integer for width with the command line when started.

Command could be /program command -width 100 filename

I've tried to do this as following but it does not compile and I'm not able to debug it successfully. Main is set to int main(int argc, char *argv[]).

my attempt

void process_file(FILE *infile,unsigned long end)
{
    char ch;
    int width,start,f_index=0;   //File index
    unsigned long bb_index=0;  // Buffer index
    //width =16; // Width is fixed as 16
    if((argc == 5) && (strcmp(argv[2],"-width") == 0))
    {
        width = atoi(argv[3]);
    }
    else
    {
        width = 16;
    }
    unsigned char *byte_buffer = malloc(width);
    start=0; // Starting at zero
    while (!feof(infile))
    {
        ch = getc(infile);
        if ((f_index >= start)&&(f_index <= end))
        {
            byte_buffer[bb_index] = ch;
            bb_index++;
        }
        if (bb_index >= width)
        {
            print(byte_buffer,bb_index,width);
            bb_index=0;
        }
    }

original function with defined (static) width which works, but does not read argv[3]

void process_file(FILE *infile,unsigned long end)
{
    char ch;
    unsigned long width,start,f_index=0;   //File index
    unsigned long bb_index=0;  // Buffer index
    width =16; // Width is fixed as 16
    unsigned char *byte_buffer = malloc(width);
    start=0; // Starting at zero
    while (!feof(infile))
    {
        ch = getc(infile);
        if ((f_index >= start)&&(f_index <= end))
        {
            byte_buffer[bb_index] = ch;
            bb_index++;
        }
        if (bb_index >= width)
        {
            print(byte_buffer,bb_index,width);
            bb_index=0;
        }
        f_index++;
    }
    if (bb_index)
        print(byte_buffer,bb_index,width);
    fclose(infile);
    free(byte_buffer);
}
Mahesh Bansod
  • 1,493
  • 2
  • 24
  • 42
John
  • 341
  • 1
  • 6
  • 13

2 Answers2

0

argc and argv aren't being passed into process_file so it wont compile.

Try parsing the command line parameters in your main function and passing in width as a parameter to process_file.

You should also reorder your parameters to have filename before width as width is optional and filename is not?

Maybe your main function could look something like.

int main(int argc, char** argv)
{
    /*
        usage program <command> <filename> -width 100
    */

    if(argc < 3)
    {
        printf("not enough args\n");
    } else
    {
        int width = 16;

        if((argc == 5) && (strcmp(argv[3],"-width") == 0))
        {
            // atoi doe not detect conversion failure use strol maybe?
            width = atoi(argv[4]);
        }

        FILE* f = fopen(argv[2], "r");

        if(f)
        {
            int arbitary_endval = 100;
            process_file(f, arbitary_endval, width);
        } else
        {
            printf("could not open file %s\n", argv[2]);
        }
    }
}

I've put 100 in as an arbitrary value for end.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • If you are working on linux, you can use gnu [getopt](http://www.gnu.org/software/libc/manual/html_node/Getopt.html) to make your command line parsing more flexible. – Paul Rooney Oct 14 '14 at 23:54
0

The program will not compile because argc and argv are undefined within the scope of the function process_file. The declaration int main(int argc, char *argv[]) is OK, but it declares argc and argv only within the body of the main function itself.

I would recommend either passing argc and argv as additional arguments to the function process_file, or parse the command line outside process_file and pass the width as an argument to that function.

Your rules for the order of things on the command-line seem a bit rigid, but if width is the only optional command-line parameter then you may be able to make do with this kind of logic.

Here is more general question about command-line options with some good answers: Parsing command-line arguments?

Community
  • 1
  • 1
David K
  • 3,147
  • 2
  • 13
  • 19