2012-10-14 35 views
1

比方說,我有一個struct稱爲test白色空間複製串用C

struct test 
{ 
    char name[16]; 

} test; 

我想讀取用戶的輸入,並把它放在name領域。假設用戶輸入"hello"作爲輸入。我的代碼是這樣的:

struct test test1; 

strcpy(test1.name, user_input); 

現在名稱中("hello")5個字符,但我希望它有它的16個字符:5的實際投入,其餘的空格。我怎樣才能做到這一點?

+1

是否有任何理由要做到這一點首先?你不應該把剩下的空白空間(最後一個字節應該是0),否則,你在使用字符串函數時可能會遇到問題。 – nhahtdh

+0

是的。我正在編寫一個與linux中的「ar」命令非常相似的程序,並且需要有固定長度的字段。 – PoweredByOrange

+0

只是讓剩下的字節爲0. – nhahtdh

回答

4

sprintf ()可以做到這一點:

sprintf(test1.name,"%-15s","John Doe"); 
printf("[%s] length of test1.name: %ld\n",test1.name,strlen(test1.name)); 
sprintf(test1.name,"%-*s",(int) sizeof(test1.name) - 1,"Jane Jones"); 
printf("[%s] length of test1.name: %ld\n",test1.name,strlen(test1.name)) 

輸出:

[John Doe  ] length of test1.name: 15 
[Jane Jones  ] length of test1.name: 15 

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

int copy_with_pad(char *destination,const char *source, int dest_size, char pad_char) 
{ 
    int pad_ctr = 0; 
    if (dest_size < 1) return -1; 
    int source_length = strlen(source); 
    int data_size = dest_size - 1; 
    destination[data_size] = '\0'; 
    int i = 0; 
    while (i < data_size) 
    { 
     if (i >= source_length) 
     { 
     destination[i] = pad_char; 
     pad_ctr++; 
     } 
     else 
     destination[i] = source[i]; 
     i++; 
    } 
    return pad_ctr; 
} 


int main(void) 
{ 
    struct test { 
     char name[16]; 
    }; 
    struct test test1; 
    int chars_padded = copy_with_pad(test1.name,"Hollywood Dan", 
            sizeof(test1.name),' '); 
    printf("%d padding chars added: [%s]\n",chars_padded,test1.name); 
    chars_padded = copy_with_pad(test1.name,"The Honorable Hollywood Dan Jr.", 
           sizeof(test1.name),' '); 
    printf("%d padding chars added: [%s]\n",chars_padded,test1.name); 
    chars_padded = copy_with_pad(test1.name,"",16,' '); 
    printf("%d padding chars added: [%s]\n",chars_padded,test1.name); 
} 

輸出

2 padding chars added: [Hollywood Dan ] 
0 padding chars added: [The Honorable H] 
15 padding chars added: [    ] 
+0

sprintf()正是我一直在尋找的! – PoweredByOrange

2

我想明顯的將是這樣的:

memset(test1.name, ' ', 16); 

size_t len = min(16, strlen(user_input)); 

memcpy(test1.name, user_input, len); 

如果你想填零任何多餘的空間,這是相當簡單一點:

strncpy(test1.name, user_input, 16); 

[我第一次已見/有人問一個問題,其中strncpy可能實際上是一個正確答案。]

+0

我寧願不用空格填充'name'字段。處理字符串時,零填充在期望方面更安全。 – nhahtdh

0
// first remember 
// that a character array of length 16 can only hold a string of 15 
// chars because it needs the trailing zero 
// this program puts in the user input (you should check that it is short enough to fit) 
// then puts in the spaces, one at a time, then the terminating zero 

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

int main() 
{ 
    char name [16];   // room for 15 chars plus a null terminator 
    char word [] = "hello"; // user input 

    strcpy(name,word);  // copy user input to name 
    int i; 
    for (i = strlen(name); i < (sizeof(name)-1); i++) { 
     name[i] = ' ';  // pad with blanks 
    } 
    name[sizeof(name)-1] = 0; // string terminator 

    printf("name = \"%s\"\n",name); 
    return 0; 
}