0

This is a small portion of my code:

char *a;
asprintf(&a,"%%%ds",Max_FnLen);
printf(a,files[i-1]->d_name);
free(a); 
printf("%s",KNRM);
if ( (i % (180/Max_FnLen)) == 0) printf("\n");

Its running fine but I want to left align the output but it is coming right aligned by default can anyone help me with this.

Kyoichi Shido
  • 109
  • 1
  • 8
  • 1
    To use a _variable_ to specfy the width, use e.g. `printf("%.*s", len, str)`. The `*` specifies a variable in the argument list is to be used for the width (or precission). – Paul Ogilvie Sep 11 '17 at 11:32

2 Answers2

0

Use the - flag, as described in the ref:

- Left-justify within the given field width; Right justification is the default (see width sub-specifier).

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

Use "-" modifier like: printf("%-20s\n", "short line");

In your case:

char *a;
asprintf(&a,"%%-%ds",Max_FnLen);
printf(a,files[i-1]->d_name);
free(a); 
printf("%s",KNRM);
if ( (i % (180/Max_FnLen)) == 0) printf("\n");
MrCryo
  • 641
  • 7
  • 16