2009-08-27 70 views
1

我想知道如何從長度爲'n'的標準輸入讀取字符串。我嘗試了使用與fgets()函數,但有一個問題,如果我提交長度的字符串>ň如何從標準輸入讀取長度爲'n'的字符串

#include <stdio.h> 

int STRING_SIZE=4; 
int getString(char *); 

int getString(char *str) 
{ 
    printf("\nEnter a string of length < %d: ", STRING_SIZE); 
    fgets(str, STRING_SIZE, stdin); 
    fflush(stdin); 
    printf("\n\n%s\n\n",str); 
    return 0; 
} 


int main(void) 
{ 

    char str1[1024]; 
    char str2[1024]; 

    getString(str1); 
    getString(str2); 

    fprintf(stdout, "%s\n", str1); 
    fprintf(stdout, "%s\n", str2); 

    return 0; 
} 

,如果我進入大小比4 STR1更多的字符串,然後剩餘的字符越來越自動分配到STR2 。

那麼,即使在給出string> STRING_SIZE之後,我還可以給str1,str2賦予字符串嗎?

我使用的是GCC編譯器4.3,如果我上面編譯源代碼

$ ./a.out 

Enter a string of length < 4: 12345678 


123 


Enter a string of length < 4: 

456 

123 
456 
+0

不明確什麼是你的目標 – qrdl 2009-08-27 12:54:47

+1

使用fflush()在輸入流是用C未定義 – 2009-08-27 15:18:11

+0

謝謝「尼爾·巴特沃思」我永遠不會試圖以這種方式再利用。 – codingfreak 2009-08-28 05:13:23

回答

3

一種策略是檢查您的字符串是否存在換行符;如果你沒有找到一個,那麼你的用戶可能會輸入一個對於目標緩衝區來說太長的字符串。在這種情況下,可以反覆調用與fgets()與第二,虛擬緩衝區爲目標,扔掉虛假輸入:

if (fgets(str, STR_SIZE, stdin) != NULL) 
{ 
    char *nl = strchr(str, '\n'); 
    if (nl == NULL) 
    { 
    /** 
    * Newline not found, input string too long for target buffer. 
    * Repeatedly read from input stream into a dummy buffer 
    * until newline is seen or fgets() returns EOF or error. 
    */ 
    char dummy[STR_SIZE]; 
    char *r; 

    printf("Warning - input string longer than expected, ignoring excess characters\n"); 

    do { 
     r = fgets(dummy, sizeof dummy, stdin); 
    } while (r != NULL && strchr(dummy, '\n') == NULL); 
    } 
    else 
    { 
    /** 
    * Input string is okay, remove newline character 
    */ 
    *nl = 0; 
    } 
} 
else 
{ 
    /** 
    * EOF or error detected on read; handle that here 
    */ 
} 
+0

感謝約翰真的非常豐富和簡單的方法.... – codingfreak 2009-08-28 05:15:03

1

確保分配str1str2正常。

char str1[STRING_SIZE]; 
char str2[STRING_SIZE]; 

另外,請記住,fgets將空字符結束的字符串,所以你真的只得到STRING_SIZE - 1字符。

+0

嗨安德魯 我只是加起來的代碼。我想我只是在你提到它的時候正確地做了。 – codingfreak 2009-08-27 15:04:50

1

在循環中使用getch。

+7

getch不是標準函數,getc/fgetc是。 – 2009-08-27 13:11:02

0

給這一個鏡頭,使用的getc()代替gets()函數。我在OS X上做了一些快速測試。我做了其他一些無關緊要的更改。你應該能夠發現它們。唯一重要的是對getString()定義的更改:請注意指定char *緩衝區大小的len參數。

#include <stdio.h> 

#define MAX_LEN 8 
#define STRING_LEN 6 

int getString(char *str, int len) 
{ 
     int n_read = 0; 
     int c; 
     int m_len = MAX_LEN; 

     if (len < m_len) m_len = len; 

     printf("\nEnter a string of length < %d: ", MAX_LEN); 

     while ((n_read < len) && (c = getc(stdin)) && c!= EOF && c!='\n') { 
       if (n_read < m_len-1) { 
         str[n_read++] = c; 
       } 
     } 
     str[n_read] = '\0'; 

     return n_read; 
} 

int main(int argc, char **argv) 
{ 
     char str1[STRING_LEN]; 
     char str2[STRING_LEN]; 

     getString(str1, STRING_LEN); 
     getString(str2, STRING_LEN); 

     fprintf(stdout, "%s\n", str1); 
     fprintf(stdout, "%s\n", str2); 

     return 0; 
} 
+0

這似乎是與我的初步測試羅布.....感謝回覆 – codingfreak 2009-08-28 04:13:53

0

與「Rob Jones」和「約翰·伯德」給出的答案,我想出了一個折衷的解決辦法。

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

#define BUF_SIZE 6 
#define STRING_SIZE 4 

/* 
* void getStringStdin(char *, int , int); 
* 
* 1: BUF  :Pointer to the array of characters where input string is to be stored. 
* 2: BUF_LEN :Is the length of the rray of characters where the string is stored.buffer where we save the string. 
* 3: STRING_LEN :Is the length of the string. 
* 
* NOTE: STRING_LEN < BUF_LEN 
* 
*/ 

getStringStdin(char *buf, int buf_len, int str_len) 
{ 

    int ch; 
    char *s; 
    int len; 

    if(str_len>=buf_len) 
    len=buf_len-1; 
    else 
    len=str_len; 

    printf ("\nEnter string of length %d(Remaining part is ignored) : ",len); 

    if((fgets(buf, len+1, stdin)) != NULL) 
    { 
    s=my_strchr(buf,'\n'); 

    if(s!=NULL) 
    { 
     *s='\0'; 
    } 
    else 
    { 
     while ((ch = getchar()) != '\n' && ch != EOF); 
    } 
    } 
} 


    int main(void) 
    { 
     int i=0; 
     char buf[BUF_SIZE]; 

     do 
     { 
      getString(buf, BUF_SIZE, STRING_SIZE); 
      printf ("\nString : %s\n", buf); 
      i++; 
     }while(i<2); 

     return 0; 
    } 
+0

如果STRING_SIZE大於BUF_SIZE會發生什麼?應該使用'len'參數來指定getString()的'buf'參數的長度(大小)。嘗試製作STRING_SIZE'7'並輸入七個或更多個字符。 – 2009-08-28 16:17:25

+0

不錯的輸入羅布瓊斯,所以我已經做了相應的代碼更改 – codingfreak 2009-09-08 11:27:20