简体   繁体   English

带填充的C文本对齐

[英]C text alignment with padding

I have a simple function to print text. 我有一个简单的功能来打印文本。

while (recvbuf[i] != '\0') {
    if (i == 0) {
        printf("%+40c", recvbuf[i]);
    }
    else {
        printf("%c", recvbuf[i]);
    }
    i++;
};

It prints text and first word(line) starts with padding 40. But what I want to do is to start every line of this with padding 40 not only first. 它打印文本,第一个单词(行)以填充40开头。但是我想做的每一行都以填充40开头。 Don't know how to do it. 不知道该怎么做。

Thanks for any help. 谢谢你的帮助。

What i need as output in CONSOLE. 我需要在控制台中输出什么。

             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd

What i have now. 我现在有什么。

             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd

I'm not sure that it's padding you want (for me, padding comes after, so I won't use this terme). 我不确定是否要填充它(对我来说,填充是紧随其后的,所以我将不使用此术语)。 You want to add some space formatting at starting of console line. 您想在控制台行的开头添加一些空间格式。

So you must define: the console width, the starting spaces you want. 因此,您必须定义:控制台宽度,所需的起始空格。

Then, for each word to print, you must calculate if the console width well be reach. 然后,对于要打印的每个单词,必须计算控制台宽度是否可以达到。 If so, you must start a new line. 如果是这样,则必须开始新的一行。

If a new line is needed, you must start it with spaces. 如果需要换行,则必须以空格开头。

#include <ctype.h>
#include <stdio.h>

int word_length(const char *word)
{
    int size = 0;
    for (; *word && !isspace(*word); ++word)   
{     
        ++size;
    }
    return size;
}

void print_word(const char *word, int size)
{
    while(size--)
        putchar(*word++);

    putchar(' ');

}

void format(int width, int spaces, const char *text)
{
    int sol = spaces;
    int current_pos = 0;

    while (*text)
    {   
        /* size of the current word */
        int word_len;

    /* if needed, print spaces */
        for (; sol != 0; --sol)
        {
            putchar(' ');
            ++current_pos;
        }

        /* get size of next word */
        word_len = word_length(text);

        if (word_len > width)
        {
            printf("error, word too long...\n");
            return;
        }


    /* see if writting current word will go over width*/
        if (current_pos + word_len > width)
        {
            sol = spaces;
            current_pos = 0;
            putchar('\n');
            continue;
        }

        /* print the current word */
        print_word(text, word_len);

        /* update the cursor position */
        current_pos += word_len + 1;

        /* position text pointer position on next word */
        text += word_len;            
        while(*text && isspace(*text))
        {
            ++text;
        }                        
    }
}

int main(void)
{
    char text[] = "fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd";
    format(45, 5, text);
    return 0;
}

Will give: 会给:

     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM