如何分离来自管道的线路。在管道中有这样的文字:
HALLO:500\n
TEST:300\N
ADAD
ADAWFFA
AFAGAGAEG
我想将管道与管道分开,因为我想将值保存在变量中。
这是我的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);
}
这是从管道中逐行读取最佳值的正确方法吗?