2014年6月30日 星期一

[pipe] pipe streams to or from a process



Use pipe stream to process a file, then output the result.




Code


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

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

if (read_fp != NULL) {
while (fscanf(read_fp, "%s", macaddr_interface) != EOF)
{
printf
("macaddr_interface = %s \n",macaddr_interface);
}

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




Test File


This file was called Interface0.txt

00:12:34:55:59:e7 192.168.0.2 0 Desktop
00:22:15:4f:40:4b 192.168.0.3 0 laptop
08:00:27:6d:d4:1a 192.168.0.4 0 good-00dbf1bd22
5c:ff:35:74:4c:5b 192.168.0.5 0 android-a6436da774239157
00:0c:29:3b:94:a4 192.168.0.6 0 PC1




Result


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

0 意見:

張貼留言