用C / C ++连接空格

时间:2012-01-14 16:01:18

标签: c

我是编程新手, 我想从一个表中提取数据,这是我们尝试过的一行:

const int buf_length = 255;
char buf[buf_length+1];
int i, count, cur = 0;
...................................................................
snprintf(buf, buf_length, "%s %s", first_child, get_name( table ));
name_list( list, cur++, buf, 1);

结果是:

01-10 Aaron
02-20 Christian
03-30 Dean

我想在行的开头插入5个空格。

因为“name_list_text”,从缓冲区中删除前导空格:

int name_list_text( list_t *list, int cur, const char *textarg,
                         int numlines, int maxwidth )
{
static const char ellipsis[] = "...";
const size_t ellipsislen = strlen( ellipsis );
int textlen;
int lastline = cur + numlines;
char textbuf[4096];
char *text = textbuf;
int width;
int breakpos;
int maxwidthpos;
int pos;

/*
 * Make a copy of the textarg, since we'll want to be able to do
 * changes to our local copy.
 */
snprintf( text, 4096, "%s", textarg );

while( cur <= lastline ) {
    /* Eat leading whitespace */
    while( isspace( *text ) )
        text++, textarg++;

    textlen = strlen( text );
    if( textlen == 0 )
        break;

    /*
     * Seek to the end of the line. This has to been done iteratively,
     * since we have to decode the UTF-8 to skip over any tail bytes.
     */
    pos = 0;
    width = 0;
    while( width < maxwidth ) {
        width++;
        /* Skip tail bytes */
        while( ( text[ ++pos ] & 0xc0 ) == 0x80 )
            ;
        if( text[pos] == '\0' ) {
            breakpos = pos;
            goto breaknow;
        }
    }
    maxwidthpos = pos;

    breakpos = -1;
    for( pos = 0; pos <= maxwidthpos; pos++ ) {
        if( isspace( text[ pos ] ) || text[ pos ] == '\0' ) {
            breakpos = pos;
            if( text[ pos ] == '\n' )
                break;
        }
    }
    if( breakpos == -1 ) {
        /* This place is as good as any to break... */
        breakpos = maxwidthpos;
    }

    if( cur == lastline ) {
        if( breakpos < textlen ) {
            /* Seek back to fit the ellipsis. */
            pos = breakpos;
            pos -= ellipsislen;
            /* Make sure to land at the start of a UTF-8 character. */
            while( ( text[ pos ] & 0xc0 ) == 0x80 )
                pos--;
            /* Write the ellipsis. Bounds have been checked. */
            strcpy (text+pos, ellipsis);
        }
    }
breaknow:
    text[ breakpos ] = '\0';
    list_set_text( list, cur++, text );
    text[ breakpos ] = textarg[ breakpos ];
    text = &text[ breakpos ];
    textarg = &textarg[ breakpos ];
    }
return cur;
}

无法使用

 "\t%s %s"

 "     %s %s"
  ^^^^^

在功能上。

期望的结果:

     01-10 Aaron
     02-20 Christian
     03-30 Dean
^^^^^

如何在缓冲区中连接(whitout更改“name_list_text”):

“5 whitespace”+“snprintf(buf,buf_length,”%s%s“,first_child,get_name(table));”

posibile解决方案是使用像ASCII空格一样的“隐形字符”:

snprintf(buf, buf_length, "\32     %s %s", first_child, get_name( table ));
name_list( list, cur++, buf, 1);

感谢。

1 个答案:

答案 0 :(得分:3)

将空格添加到格式字符串?

snprintf(buf, buf_length, "     %s %s", first_child, get_name( table ));
                           ^^^^^
缓冲区大小参数可能需要

buf_length + 5


如果你的功能删除空格,你可以做两件事:

1)更改功能。

在你调用函数之前

2)print(" ");