c语言从stdin读入

代码

#include
#include

int
main(int argc, char* argv[])
{
    char * line = NULL;
    size_t len = 0;
    ssize_t read_len;
    while ((read_len=getline(&line, &len, stdin)) != -1) 
    {   
        if (read_len > 0 && line[read_len-1] == '\n')
        {   
            line[read_len-1] = '\0';
            read_len -= 1;  
        }   
        printf("%s\n", line);

    }   
    return 0;
}

参考

https://blog.csdn.net/lichengyu/article/details/41621099

你可能感兴趣的:(Linux,c语言)