2016-12-14 34 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

相关问题