Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2764746
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类: LINUX

2014-04-13 10:31:44

我在网上搜了半天getline()函数,大多针对C++的,重载函数比较多,云里雾 里的,而且没有实例,反正就是没有自己所需要的getline()函数。所以,自己在Linux下man了一把,并做了测试。getline()函数的功 能是从文件中获取行信息,即每次读取一行信息。

    因为我使用getline()函数的目的是获取本地网卡信息,即eth0的信息,从而判断启动机子时是否查了网线(本来可以从驱动里做,但应用层可以搞定,就不想多做处理了,谅解)。

 

//函数原型

#define _GNU_SOURCE

#include

      ssize_t getline(char **lineptr, size_t *n, FILE *stream);

       ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE*stream);

 

[root@localhost for_test]# cat dev

Inter-|   Receive                                                | Transmit

 face |bytes   packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carriercompressed

   lo:       0       0   0    0    0    0          0         0        0      0    0    0   0     0       0         0

 eth0:  53311     230    0    0   0     0          0        0     5370      33   0    0    0    0       0          0


[root@localhost for_test]# cat eth0_dev.c


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.     FILE *fp = NULL;
  6.     int cnt = -1;
  7.     int len = 0;
  8.     char buf1[16] = {0}, buf2[16] = {0}, buf3[16] = {0};
  9.     char *line = NULL;
  10.     char *pstr = NULL;    
  11.     fp = fopen("./dev", "rb");
  12.     if(NULL == fp)
  13.     {
  14.         printf("open /proc/net/dev err!\n");
  15.         return -1;
  16.     }
  17.     while(-1 != (cnt = getline(&line, &len, fp)))//读取行信息,'\n'为换行标志
  18.     {
  19.         pstr = strstr(line, "eth0");//查找改行中是否有"eth0"的字符串
  20.         if(NULL != pstr)
  21.         {
  22.             //printf("%s\n", pstr);
  23.             sscanf(pstr, "%s\t%s\t%s", buf1, buf2, buf3);
  24.             printf("buf1:%s buf2:%s buf3:%s\n", buf1, buf2, buf3);
  25.             break;
  26.         }
  27.     }
  28.     //确保空间的释放
  29.     if(line)
  30.     {
  31.         free(line);
  32.     }
  33.     fclose(fp);
  34.     return 0;
  35. }



[root@localhost for_test]#gcc eth0_dev.c

[root@localhost for_test]# ./a.out

buf1:eth0:  buf2:53311 buf3:230


[root@localhost for_test]# man getline


点击(此处)折叠或打开

  1. DESCRIPTION
  2.        getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null-
  3.        terminated and includes the newline character, if one was found.

  4.        If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should be freed by the user program. Alterna-
  5.        tively, before calling getline(), *lineptr can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer is not
  6.        large enough to hold the line, getline() resizes it with realloc(), updating *lineptr and *n as necessary. In either case, on a suc-
  7.        cessful call, *lineptr and *n will be updated to reflect the buffer address and allocated size respectively.

  8.        getdelim() works like getline(), except a line delimiter other than newline can be specified as the delimiter argument. As with get-
  9.        line(), a delimiter character is not added if one was not present in the input before end of file was reached.

  10. RETURN VALUE
  11.        On success, getline() and getdelim() return the number of characters read, including the delimiter character, but not including the
  12.        terminating null byte. This value can be used to handle embedded null bytes in the line read.

  13.        Both functions return -1 on failure to read a line (including end of file condition).

  14. ERRORS
  15.        EINVAL Bad parameters (n or lineptr is NULL, or stream is not valid).

  16. EXAMPLE
  17.        #define _GNU_SOURCE
  18.        #include <stdio.h>
  19.        #include <stdlib.h>

  20.        int main(void)
  21.        {
  22.             FILE * fp;
  23.             char * line = NULL;
  24.             size_t len = 0;
  25.             ssize_t read;
  26.             fp = fopen("/etc/motd", "r");
  27.             if (fp == NULL)
  28.                  exit(EXIT_FAILURE);
  29.             while ((read = getline(&line, &len, fp)) != -1) {
  30.                  printf("Retrieved line of length %zu :\n", read);
  31.                  printf("%s", line);
  32.             }
  33.             if (line)
  34.                  free(line);
  35.             return EXIT_SUCCESS;
  36.        }

  37. CONFORMING TO
  38.        Both getline() and getdelim() are GNU extensions. They are available since libc 4.6.27.



阅读(1507) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~