[C++]Linux之读取计算机网络数据[/proc/net/dev]

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

#define BUFFER_SIZE 256

int main(){
	FILE *stream;
	char buffer[BUFFER_SIZE];//缓冲区
	char *line_return;//记录每次返回值(行)
	int line_count = 0;//记录行数
	char tmp_itemName[32];//临时存放文件中的每行的项目名称
	int itemReceive;//存放每一个网卡的接受到的字节总数(单位:Byte)
	int itemTransmit;//存放每一个网卡的已发送的字节总数(单位:Byte)
	
	system("cat /proc/net/dev");	
	printf("**************************************************************************************************************");

	stream = fopen("/proc/net/dev", "r");

	line_return = fgets (buffer, BUFFER_SIZE * sizeof(char), stream);//读取第一行	
	line_count++;
	printf("\n[MAIN] line %d: %s\n", line_count, line_return);//输出第一行
	
	line_return = fgets (buffer, BUFFER_SIZE * sizeof(char), stream);//读取第二行	
	line_count++;
	printf("\n[MAIN] line %d: %s\n", line_count, line_return);//输出第二行
	printf("**************************************************************************************************************");
	
	while(line_return != NULL){
		line_return = fgets (buffer, BUFFER_SIZE * sizeof(char), stream);
		line_count++;
		printf("\n[MAIN] line %d: %s", line_count, line_return);
		sscanf(	buffer, 
			"%s%d%d%d%d%d%d%d%d%d", 
			tmp_itemName, 
			&itemReceive, 
			&itemTransmit, 
			&itemTransmit, 
			&itemTransmit, 
			&itemTransmit, 
			&itemTransmit, 
			&itemTransmit, 
			&itemTransmit, 
			&itemTransmit);	
		printf("line %d:itemReceive: %d\n", line_count, itemReceive);
		printf("line %d:itemTransmit: %d\n\n", line_count, itemTransmit);
	}	
	
	return 0;
}

 

posted @ 2017-12-08 23:56  千千寰宇  阅读(1268)  评论(0编辑  收藏  举报