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);
}