3

This is the part of code I'm working on:

void update_log(char *name, pthread_t ID, char *status, char *update)
{
    time(&rawtime);
    time_str = asctime(localtime(&rawtime));
    time_str[strlen(time_str) - 1] = 0;
    fprintf(log_file, "[%s]  [%12s]  [%u]  [%s]  %s\n", time_str, name, (unsigned int)ID, status, update);
}  

The output is:

[Sat Mar  9 21:36:20 2013]  [        main]  [197777152]  [OK]  ******
[Sat Mar  9 21:36:20 2013]  [update_table]  [172680960]  [OK]  **********
[Sat Mar  9 21:36:22 2013]  [update_table]  [172680960]  [OK]  ******
[Sat Mar  9 21:36:25 2013]  [        main]  [197777152]  [OK]  ****************

Is there a way to make the output of the name variable look like this(so it would still take 12 blocks and still would be in brackets):

[Sat Mar  9 21:36:20 2013]  [main]          [197777152]  [OK]  ******
[Sat Mar  9 21:36:20 2013]  [update_table]  [172680960]  [OK]  **********
[Sat Mar  9 21:36:22 2013]  [update_table]  [172680960]  [OK]  ******
[Sat Mar  9 21:36:25 2013]  [main]          [197777152]  [OK]  ****************

I was thinking about adding brackets to name variable before fprintf() function, but is there a easy way in C to add a character to the beginning of the string?

Thanks.

4d4c
  • 8,049
  • 4
  • 24
  • 29
  • By the way, the `char *` arguments to these functions should be `const char *`. –  Mar 10 '13 at 06:52

3 Answers3

3

This seems pretty easy to me.

#include <stdio.h>

int main()
{
    char buffer[16];

    sprintf(buffer,"[%s]", "main");
    printf("[0] %-14s [2]\n", buffer);

    sprintf(buffer,"[%s]", "update_table");
    printf("[0] %-14s [2]\n", buffer);

    sprintf(buffer,"[%s]", "main");
    printf("[0] %-14s [2]\n", buffer);

    return 0;
}

Output

[0] [main]         [2]
[0] [update_table] [2]
[0] [main]         [2]
enhzflep
  • 12,927
  • 2
  • 32
  • 51
  • No `sprintf()`, please! I don't want another question to be asked about buffer overrun errors... –  Mar 10 '13 at 06:54
  • It's up to you whether you answer them or not. The printf field width specifier indicates the string to be no more than 12 chars, admittedly, it's lazy coding - I should have used strlen then malloc, followed with strcat(x3) and finally by free, it does work as intended, however. But please, for the love of anything precious - DO NOT post solutions which provide the wrong answer. Did you try running your code? It's quite clearly and obviously incorrect. Did you miss the part where the ***entire purpose*** was to left-justify a value that's printed inside square brackets? Yours, H2S04 + HNO3... – enhzflep Mar 10 '13 at 07:10
2

You can add brackets to the beginning and end of name variable first, and then print name as %12s as you have done, but left-justify instead of right-justify(which is the default)

You can use strcat to add brackets at the beginning and end. See: How do I concatenate const/literal strings in C?

And this link shows how to left-justify: (See the flags table) http://www.cplusplus.com/reference/cstdio/printf/

Community
  • 1
  • 1
Neha Karanjkar
  • 3,390
  • 2
  • 29
  • 48
1

I don't see any easy way, but you can just make another string out of it:

void update_log(char *name, pthread_t ID, char *status, char *update)
{
    time(&rawtime);
    time_str = asctime(localtime(&rawtime));
    time_str[strlen(time_str) - 1] = 0;

    size_t len = strlen(name);
    char real_name[len + 3];
    real_name[0] = '[';
    strncpy(real_name + 1, name, sizeof(real_name));
    real_name[len + 1] = ']';
    real_name[len + 2] = 0;

    fprintf(log_file, "[%s]  [%12s]  [%u]  [%s]  %s\n", time_str, real_name, (unsigned int)ID, status, update);
}