在C中逐行从管道读取 [英] Read from pipe line by line in C

查看:148
本文介绍了在C中逐行从管道读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何分隔来自管道的线.管道中有以下文字:

How can I separate the lines which are coming from a pipe. In the pipe there is this text:

HALLO:500\n
TEST:300\N
ADAD
ADAWFFA
AFAGAGAEG

我想将行与管道分开,因为我想将值保存在变量中.

I want to separate the lines from the pipe because i want to save the values in variables.

这是我的c代码:

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

#define BUFFERSIZE    1

int main(int argc, char **argv){
    unsigned char     buffer[BUFFERSIZE];
    FILE                         *instream;
    int                            bytes_read=0;
    int                            buffer_size=0;


    buffer_size=sizeof(unsigned char)*BUFFERSIZE;
    /* open stdin for reading */
    instream=fopen("/dev/stdin","r");

    /* did it open? */
    if(instream!=NULL){
        /* read from stdin until it's end */
        while((bytes_read=fread(&buffer, buffer_size, 1, instream))==buffer_size){
            fprintf(stdout, "%c", buffer[0]);
        }
    }
    /* if any error occured, exit with an error message */
    else{
        fprintf(stderr, "ERROR opening stdin. aborting.\n");
        exit(1);
    }

    return(0);
}

这是从管道中逐行读取最佳路线的正确方法吗?

Is this the right way to read from pipe for the best line by line?

推荐答案

通常称为从stdin中读取.程序不必在乎输入是管道,重定向文件还是键盘.

This is usually just called reading from stdin. The program shouldn't care whether the input is a pipe, a redirected file, or a keyboard.

fread只会读取直到缓冲区已满.使用fgets读取一行.

fread will just read until the buffer is full. Use fgets to read a line.

此外,缓冲区大小应足够大以容纳该行.对于一次性的小程序,您只需选择一个数字即可.或者有一个标准名称BUFSIZ,它为您提供了一个缓冲区.多大?足够大.真的吗?可能.

Also the buffer size should be big enough to hold the line. For little one-off programs, you can just pick a number. Or there's a standard name BUFSIZ which gives you a pretty-big buffer. How big? Big enough. Really? Probably.

fgets将在字符串中复制换行符,除非该字符串先填充.因此,您可以测试最后一个字符,以判断该行是否被截断.有了合理的投入,这种情况就不会发生.但是,更可靠的方法是分配更大的缓冲区,复制部分行,然后再次调用fgets tp继续尝试获取完整行.

fgets will copy the newline character in the string unless the string fills up first. So you can test the last character to tell if the line was truncated or not. With reasonable inputs, that's not going to happen. But a more robust approach would allocate a larger buffer, copy the partial line, and call fgets again tp keep trying to get a complete line.

#include <stdio.h>

int main() {
    char buf[BUFSIZ];
    fgets(buf, sizeof buf, stdin);
    if (buf[strlen(buf)-1] == '\n') {
        // read full line
    } else {
        // line was truncated
    }
    return 0;
}

这将使您免受可怕的缓冲区溢出问题的保护. fgets的写操作不会超过传递给它的大小.如上所述,另一半正在对可能由非预期的长输入线导致的局部线做一些明智的选择.

This gets you halfway to being protected from the dreaded buffer overflow problem. fgets will not write more than the size passed to it. The other half, as mentioned above, is doing something sensible with the possible partial lines that may result from unexpectedly long input lines.

这篇关于在C中逐行从管道读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆