2014年6月30日 星期一

[pipe] Store pipe stream into buffer




code


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

int main()
{
FILE
*read_fp;
char tmp[BUFSIZ + 1];
char buffer[BUFSIZ + 1];
char macaddr_interface[18]={0};
memset
(buffer, '\0', sizeof(buffer));
read_fp
= popen("cat /home/freeman/test/test100/Interface0.txt | awk 'NR >1 {print $1;}'", "r");

if (read_fp != NULL) {
while (fscanf(read_fp, "%s", macaddr_interface) != EOF)
{
/* - : Left-justify within the given field width */
sprintf
(tmp,"%-18s",macaddr_interface);
strcat
(buffer,tmp);
}

printf
("%s",buffer);

pclose
(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}




Result


00:22:15:4f:40:4b 08:00:27:6d:d4:1a 5c:ff:35:74:4c:5b 00:0c:29:3b:94:a4 


Reference:



  1. C String Concatenation

  2. printf


0 意見:

張貼留言