2015-01-15 96 views
0

我正在尋找正確縮進下面,我想DG1, DG2, DG3, DG4應該在同一列,是否有一個最佳的方式呢?謝謝,任何指針或幫助將不勝感激。C printf壓痕問題


 Test                 DG1                 
                     DG2                  
                     DG3                  
                     DG3                  

SH-4.2#^ C

#include <stdio.h> 

int main() 
{ 
    printf("%-64s\t%s\n", "List", "Bindings"); 
    printf("-----------------------------------------------------------------------------\n"); 

    const char *curr_dl_result[] = { 
     [0] = "DG1", 
     [1] = "DG2", 
     [2] = "DG3", 
     [3] = "DG3", 
    }; 
    int dg_count = 4; 
    int j = 0; 
    char buff[1024]; 
    printf("%s", "Test"); 
    if(dg_count) { 
     unsigned char is_print_done = 0;  
     for(j = 0; j < dg_count; j++) {  
      printf("%78s\n", (curr_dl_result[j])); 
      is_print_done = 1; 
     } 
    } else { 
     printf("%78s\n","-"); 
    } 
return 0; 
} 
+1

不要忘了printf和sprintf的返回字符的打印,所以你可以使用該號碼在該行年底前加空格正確數量的金額你「DG1」串並獲得更好的輸出。 – Johan

+0

這是一個很好的建議,會嘗試。 – user2766839

+0

感謝大家的快速幫助,我可能會去做以下事情:if(j == 0){int ty-space = 74; printf(「%* s \ n」,space,curr_dl_result [j]); } – user2766839

回答

1

也許使用的printf返回打印的字符數的事實, 然後墊在打印最後一個字符串之前用空格。

#include <stdio.h> 

void my_out(const char* str1, const char* str2) 
{ 
    int len = printf("%s", str1); 
    int i; 
    for(i=len ; i<60 ; i++) 
    { 
     printf(" "); 
    } 
    printf("%s\n", str2); 
} 

int main() 
{ 

    my_out("List", "Bindings"); 
    printf("-----------------------------------------------------------------------------\n"); 

    const char *curr_dl_result[] = { 
     [0] = "DG1", 
     [1] = "DG2", 
     [2] = "DG3", 
     [3] = "DG3", 
    }; 

    my_out("Test",curr_dl_result[1]); 
    my_out("Test eee",curr_dl_result[2]); 
    my_out("Test 22",curr_dl_result[3]); 
    return 0; 
} 

這將打印:

List              Bindings 
----------------------------------------------------------------------------- 
Test              DG2 
Test eee             DG3 
Test 22              DG3 
0

例如本if更換內部forprintf應該做的伎倆:

if (j == 0) 
    printf("%74s\n", (curr_dl_result[j])); // account for "Test" (4 chars) on same line 
else 
    printf("%78s\n", (curr_dl_result[j])); 
+0

那麼,「測試」只是一個例子,它可能是任何東西,試圖找出是否有內置的方式。 – user2766839

+0

@ user2766839從「78 - strlen(」Test「)== 74'的觀察開始進行概括是非常簡單的。請務必在你的問題中陳述你希望通過首先詢問他們而找到的東西:) – Drux

1

你爲什麼不改變

printf("%s", "Test");

printf("%s\n", "Test"); 

這將打印和縮進DG1,DG2,DG3, DG4正確,如果您不介意,將在測試後打印一行 看到這個http://ideone.com/ZhOlhF

0

怎麼樣動態分配(仍然有一些硬編碼的東西,但更普及):

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() 
{ 
    printf("%-64s\t%s\n", "List", "Bindings"); 
    printf("-------------------------------------------------------------------------------\n"); 

    const char *curr_dl_result[] = { 
     [0] = "DG1", 
     [1] = "DG2", 
     [2] = "DG3", 
     [3] = "DG3", 
    }; 
    int dg_count = 4; 
    int j = 0; 
    int pos = printf("%s", "Test"); 
    if(dg_count) { 
     unsigned char is_print_done = 0; 
     for(j = 0; j < dg_count; j++) { 
      char *spacer = malloc(74-pos*sizeof(char)); 
      memset(spacer, ' ', 74-pos); 
      printf("%s", spacer); 
      printf("%s\n", (curr_dl_result[j])); 
      is_print_done = 1; 
      pos = 0; 
      free(spacer); 
     } 
    } 
    else { 
     printf("%78s\n","-"); 
    } 
    return 0; 
}