0
我是新來編程, 我想從一個表中,我們已經嘗試過的線路之一提取數據:串聯空白與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_list(list, cur++, buf, 1);
這是結果:
01-10 Aaron
02-20 Christian
03-30 Dean
我想在行的beginnig處插入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空白」 +「的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);
感謝。
這是一個有點混亂,你的問題實際上是什麼了。你能展示更多的代碼和評論你的每個函數在做什麼? – Pubby 2012-01-14 16:10:30