I am new to programming, I want to extract data from one table, one of the lines we have tried:
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个白色空间。
Because "name_list_text", removes the leading whitespace from buffer:
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] == ) {
breakpos = pos;
goto breaknow;
}
}
maxwidthpos = pos;
breakpos = -1;
f或( pos = 0; pos <= maxwidthpos; pos++ ) {
if( isspace( text[ pos ] ) || text[ pos ] == ) {
breakpos = pos;
if( text[ pos ] ==
)
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 ] = ;
list_set_text( list, cur++, text );
text[ breakpos ] = textarg[ breakpos ];
text = &text[ breakpos ];
textarg = &textarg[ breakpos ];
}
return cur;
}
can t use
" %s %s"
或
" %s %s"
^^^^^
执行中。
预期成果:
01-10 Aaron
02-20 Christian
03-30 Dean
^^^^^
• 如何在缓冲中进行组装(冲变“姓名_list_text”):
“5 白色空间” + “snprintf(buf, buf_length, “%s %s %s”, 首先是儿童,获得姓名(表格);”
可行解决办法是使用ASCII空间等“无形的果园”:
snprintf(buf, buf_length, "32 %s %s", first_child, get_name( table ));
name_list( list, cur++, buf, 1);
感谢。