2016-12-14 35 views
-2

我想交互式 (我想)從標準輸入讀取一行直到EOF,但在每行之後如果行首字符是'+'然後打印"OK"其他打印"NOT OK"。我試過這段代碼,但即使輸入的第一個字符的第一個字符等於'+',也會打印"NOT OK"從標準輸入讀取直到EOF並在測試第一個字符後打印文本

int main() 
{ 
    #define BUF_SIZE 1024 
    char buffer[BUF_SIZE]; 
    size_t contentSize = 1; 
    /* Preallocate space. We could just allocate one char here, 
    but that wouldn't be efficient. */ 
    char *content = malloc(sizeof(char) * BUF_SIZE); 
    if(content == NULL) 
    { 
     perror("Failed to allocate content"); 
     exit(1); 
    } 
    content[0] = '\0'; // make null-terminated 
    while(fgets(buffer, BUF_SIZE, stdin)) 
    { 
     char *old = content; 
     contentSize += strlen(buffer); 
     content = realloc(content, contentSize); 
     if(content == NULL) 
     { 
      perror("Failed to reallocate content"); 
      free(old); 
      exit(2); 
     } 
     strcat(content, buffer); 
     if (content[0]== '+') { 
      printf("OK\n"); 
     } else { 
      printf("NOT OK\n"); 
     } 
    } 

    if(ferror(stdin)) 
    { 
     free(content); 
     perror("Error reading from stdin."); 
     exit(3); 
    } 
} 
+0

問題是?順便說一句,你的''鑰匙壞了嗎? – alk

+0

是這個代碼打印不好,即使在第一個字符是'+' – mardon

+0

OT:你能確定沒有輸入的行比1023'char's更長嗎? – alk

回答

1

您串聯緩衝區內容

strcat(content, buffer); 

所以對於第一個輸入,假設「ABC」 contentABC,它將打印也不行。 對於第二個輸入,假設「+ xyz」content將爲abc + xyz所以content[0]的值將始終爲「a」,因此它總是會打印NOT OK。

類似地,如果您的第一個輸入是「+ abc」,那麼它將始終爲所有輸入打印確定。

使用的strcpy,而不是strcat的

strcpy(content, buffer); 
+0

我評論行'內容[0] ='\ 0';'要麼腳本pritn後任何輸入行不行,即使第一個字符是+ – mardon

+0

對不起,不需要評論這一點。我編輯了我的評論。執行並確認這個 –

2

要通過fgets()讀取,更好地處理這個作爲一個獨立的功能@alk

如下提示代碼類似於OP的。一個關鍵的區別是測試fgets(buffer)是否讀取'\n'

#include <math.h> 
#include <stdio.h> 
#define BUF_SIZE 10 

char *readline_mardon(void) { 
    char buffer[BUF_SIZE]; 
    size_t contentSize = 1; 
    char *content = malloc(contentSize); 
    if (content == NULL) { 
    perror("Failed to allocate content"); 
    exit(1); 
    } 
    content[0] = '\0'; // make null-terminated 
    while (fgets(buffer, sizeof buffer, stdin)) { 
    size_t buffer_length = strlen(buffer); 

    // more idiomatic code 
    // Assign `content` after successful allocation detected 
    size_t contentSize_new = contentSize + buffer_length; 
    printf("%zu <%s>\n", buffer_length, buffer); 
    char *content_new = realloc(content, contentSize_new); 
    if (content_new == NULL) { 
     perror("Failed to reallocate content"); 
     free(content); 
     exit(2); 
    } 

    // memcpy faster than strcat as the end of the first part is known 
    memcpy(content_new + contentSize - 1, buffer, buffer_length + 1); 

    content = content_new; 
    contentSize = contentSize_new; 

    // look for \n 
    if (buffer_length > 0 && buffer[buffer_length - 1] == '\n') { 
     break; 
    } 
    } 
    return content; 
} 

使用

char *s; 
while((s = readline_mardon()) != NULL) { 
    if (s[0]== '+') { 
    printf("OK\n"); 
    } else { 
    printf("NOT OK\n"); 
    } 
    free(s); 
} 

附加代碼可能返NULL如果沒有讀取或發生輸入錯誤。

+0

我試試這個,但是偶數行有第一個字母+ NOT OK正在打印,當輸入行在10個字符以下時我得到realloc錯誤 – mardon

相關問題