English 中文(简体)
C.C.++ 固定的白色空间
原标题:Concatenate white space with C/C++
  • 时间:2012-01-14 16:01:17
  •  标签:
  • c

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);

感谢。

问题回答

将白色空间添加到形式上?

snprintf(buf, buf_length, "     %s %s", first_child, get_name( table ));
                           ^^^^^

buf_length + 5>s may be required for thekir small debate.


If your function removes white space, 2 things you can do:

(1) 改变职能。

2) print(" "); before you call the function





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签