LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to print a line of fill character in C? (https://www.linuxquestions.org/questions/programming-9/how-to-print-a-line-of-fill-character-in-c-807978/)

bvkim 05-15-2010 04:26 AM

How to print a line of fill character in C?
 
As you know, in C++ you can use setfill(char c); and setwidth( int length) to fill a line of a character.
It's line making a line of a character for output

Code:

NAME          DEPARTMENT          LOCATION
==========================================
Pete          R&D                  Chicago
...
==========================================

As you see, a line of a '=' character drawn to output.

I'm wondering, how can I draw this line in C using printf() to format?
( no loop or repetiton )

BenCollver 05-15-2010 08:04 AM

You may need to write your own function, but I am not sure how to do it without leaking memory. It is not pretty but you can find an example on rosettacode.

http://rosettacode.org/wiki/Repeat_a_string#C

Sergei Steshenko 05-15-2010 10:21 AM

Quote:

Originally Posted by bvkim (Post 3969304)
...
I'm wondering, how can I draw this line in C using printf() to format?
( no loop or repetiton )

Somewhere there is/will be a loop doing this, so why not just write it yourself ?

graemef 05-15-2010 11:05 AM

Would memset() work? You can use it to initialise a character array which can then be printed using printf().

BenCollver 05-15-2010 11:19 AM

Code:

#define repeat1max 1024
char *repeat1(char c, int w) {
    static char retval[repeat1max];
    if (w < 0 || w > repeat1max) {
        return NULL;
    }
    memset(retval, c, w);
    retval[w] = '\0';
    return retval;
}

char *repeat2(char c, int w) {
    char *retval = malloc(w);
    if (retval == NULL) {
        return NULL;
    }
    memset(retval, c, w);
    retval[w] = '\0';
    return retval;
}

printf("foobar\n%s\nfour\n%s\n", repeat1('=', 6), repeat1('=', 4));
printf("barfoo\n%s\nurfo\n%s\n", repeat2('=', 6), repeat2('=', 4));

In this example:
repeat1() fails.
repeat2() leaks.

Sergei Steshenko 05-15-2010 11:45 AM

Quote:

Originally Posted by BenCollver2 (Post 3969703)
[code]
...
repeat2() leaks.

Well, C99 has dynamic arrays, so maybe a non-leaking version can be created if to enclose 'printf' into a pair of curlies (scope) and in that scope to declare a dynamic array whose size is inherited from outer scope.

devnull10 05-15-2010 02:10 PM

One would of course have to allocate the memory first if using memset. You would probably want to write your own function which would do it, print it to screen and then free up the memory.

graemef 05-15-2010 07:41 PM

why not print the details in you function and then free the memory in the function.

devnull10 05-16-2010 12:50 AM

Quote:

Originally Posted by graemef (Post 3970134)
why not print the details in you function and then free the memory in the function.

Exactly what I said... :)

Quote:

You would probably want to write your own function which would do it, print it to screen and then free up the memory.


All times are GMT -5. The time now is 09:32 AM.