将空白与C/C+连接+;
我是编程新手, 我想从一个表中提取数据,这是我们尝试过的一行:将空白与C/C+连接+;,c,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_li
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\u list\u 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;
}
或
在功能上
预期结果:
01-10 Aaron
02-20 Christian
03-30 Dean
^^^^^
如何在缓冲区中连接(省略更改“名称列表文本”):
“5个空格”+“snprintf(buf,buf_长度,“%s%s”,第一个子,get_名称(表));”
可行的解决方案是使用“不可见字符”,如ASCII空格:
snprintf(buf, buf_length, "\32 %s %s", first_child, get_name( table ));
name_list( list, cur++, buf, 1);
谢谢。是否将空白添加到格式字符串中
snprintf(buf, buf_length, " %s %s", first_child, get_name( table ));
^^^^^
缓冲区大小参数可能需要buf_length+5
如果函数删除了空白,则可以执行以下两项操作: 1) 改变功能
2)
打印(“”)
在调用函数之前是否向格式字符串中添加空格
snprintf(buf, buf_length, " %s %s", first_child, get_name( table ));
^^^^^
缓冲区大小参数可能需要buf_length+5
如果函数删除了空白,则可以执行以下两项操作: 1) 改变功能
2)
打印(“”)代码>在调用函数之前有点混淆了问题的实质。你能展示更多的代码和注释你的每一个函数都在做什么吗?这有点让人困惑你的问题到底是什么。你能展示更多的代码和注释你的每个函数在做什么吗?
snprintf(buf, buf_length, " %s %s", first_child, get_name( table ));
^^^^^