使用C语言打印水平空格

时间:2012-01-14 09:45:21

标签: c

在这部分代码中,我想插入空白区域进行打印:

(“tvtime_osd_list_set_multitext”删除前导空格)

行:

        snprintf(buf, buf_length, "     %s %s", end_time, xmltv_get_next_title( xmltv ) ? xmltv_get_next_title( xmltv ) : "");     
        tvtime_osd_list_set_multitext( osd, cur++, buf, 1);

代码:

 int epg_show_nowandnext( tvtime_osd_t* osd, int page, station_mgr_t *stationmgr, xmltv_t *xmltv )
{
    if (!page)
        return 0;
    if ( xmltv ){
        const int buf_length = 255;
        const int max_num_lines = 15;
        const int num_station_per_page = max_num_lines / 3;
        const int num_stations = station_get_num_stations(stationmgr);
        char *old_channel = strdup(xmltv_get_channel(xmltv));
        char buf[buf_length+1];
        int i, count, cur = 0;


    /* List Header */
    snprintf(buf, buf_length, "Now showing and next on (%d/%d):", page, num_stations/num_station_per_page);
    tvtime_osd_list_set_text( osd, cur++, buf);
    tvtime_osd_list_set_hilight(osd, -1);   

    for (i = (page-1) * num_station_per_page, count = 0; (i < num_stations) && (count < num_station_per_page ); i++, count++) {
        const char *xmltv_id = 0;
        if (!(xmltv_id = station_get_xmltv_id( stationmgr, i))) 
            xmltv_id = xmltv_lookup_channel(xmltv, station_get_name( stationmgr, i));
        xmltv_set_channel( xmltv, xmltv_id);
        xmltv_refresh( xmltv );
        /* Channel number + name */
        snprintf(buf, buf_length, "[%s] %s:", station_get_channel(stationmgr,i), station_get_name( stationmgr, i));     
        tvtime_osd_list_set_multitext( osd, cur++, buf, 1);

        if (xmltv_get_title( xmltv )) {
            char start_time[50];
            char end_time[50];
            time_t start_timestamp = xmltv_get_start_time( xmltv );
            time_t end_timestamp = xmltv_get_end_time( xmltv );
            strftime( start_time, 50, "%H:%M", localtime( &start_timestamp ) );
            strftime( end_time, 50, "%H:%M", localtime( &end_timestamp ) );


            /* Highlight if current channel */
            if (station_get_current_id(stationmgr) == i+1)
                tvtime_osd_list_set_hilight(osd, cur-1);    

            /* starttime of current program + Now showing program */
            snprintf(buf, buf_length, "     %s %s", start_time, xmltv_get_title( xmltv ));      
            if (xmltv_get_sub_title( xmltv )){
                strncat(buf," (",buf_length-strlen(buf));  
                strncat(buf,xmltv_get_sub_title( xmltv ),buf_length-strlen(buf));
                strncat(buf,")",buf_length-strlen(buf));  
            }
            tvtime_osd_list_set_multitext( osd, cur++, buf, 1);

            /* endtime of current programme + Next program */       
            snprintf(buf, buf_length, "     %s %s", end_time, xmltv_get_next_title( xmltv ) ? xmltv_get_next_title( xmltv ) : "");      
            tvtime_osd_list_set_multitext( osd, cur++, buf, 1);

        } else {
            /* No program information available for this channel */
            tvtime_osd_list_set_text( osd, cur++, "");
            tvtime_osd_list_set_text( osd, cur++, "");
        }

    }
    tvtime_osd_list_set_lines( osd, cur );
    tvtime_osd_show_list( osd, 1, 1 );

    xmltv_set_channel(xmltv, old_channel);
    free(old_channel);
    xmltv_refresh( xmltv );

    if (cur > 1){
        return page;
    } else {
        return 0;
    }
} else {
    tvtime_osd_list_set_text( osd, 0, "No program information available" );
    tvtime_osd_list_set_lines( osd, 1 );
    tvtime_osd_show_list( osd, 1, 1 );
    return !page;
    }
}

此印刷品采用以下形式:

[05] TV5MONDE:
09:30 La Grange De Meslay Festival:Pavel Haas Quartet
10:00 TV5Monde Le Journal
[06] RAI UNO

我想用水平空格打印这种格式:

[05] TV5MONDE:
    09:30 La Grange De Meslay Festival:Pavel Haas Quartet
    10:00 TV5Monde Le Journal
[06] RAI UNO

我在代码中放了'*'

/* showing now  + next showing */       
snprintf(buf, buf_length, "*     %s %s", end_time, xmltv_get_next_title( xmltv ) ? xmltv_get_next_title( xmltv ) : "");     
tvtime_osd_list_set_multitext( osd, cur++, buf, 1);

输出结果(对我来说很难看):

[05] TV5MONDE:
*   09:30 La Grange De Meslay Festival:Pavel Haas Quartet
*   10:00 TV5Monde Le Journal
[06] RAI UNO

开个玩笑,怎么让char'*'看不见?

4 个答案:

答案 0 :(得分:1)

查看各种输出修饰符,特别是字段宽度:http://www.cplusplus.com/reference/clibrary/cstdio/printf/

%10s之类的东西可以解决问题。

或者,您可以在行的开头添加制表符:'\t'

答案 1 :(得分:1)

buf包含空格。 tvtime_osd_list_set_multitext是否可能删除前导空格?


根据你的评论,tvtime_osd_list_set_multitext删除了前导空格,这就是导致问题的原因。要解决它,也许您可​​以使用另一个函数来打印空白,或者您可以尝试通过在行的开头打印某种不可见的ASCII字符来“欺骗”它。

答案 2 :(得分:0)

您可以使用选项卡转义码:\ t。例如:

printf("\tHello World!");

答案 3 :(得分:0)

非常有趣的问题。你在哪里收集输出?一种可能的解释是在接收输出的终端/控制台上包装初始空格。一些“内置”IDE控制台窗口会出现这种错误(特别是在过去)。

无论如何,我会根据空格替换你的陈述:

snprintf(buf, buf_length, "     %s %s", end_time, xmltv_get_next_title( xmltv ) ? xmltv_get_next_title( xmltv ) : "");

snprintf(buf, buf_length, "\t%s %s", end_time, xmltv_get_next_title( xmltv ) ? xmltv_get_next_title( xmltv ) : "");