我在网上搜了半天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

#include

#include

int main(void)

{

FILE *fp = NULL;

int cnt = -1;

int len = 0;

char buf1[16] = {0}, buf2[16] = {0}, buf3[16] = {0};

char *line = NULL;

char *pstr = NULL;

fp = fopen("./dev", "rb");

if(NULL == fp)

{

printf("open /proc/net/dev err!\n");

return -1;

}

while(-1 != (cnt = getline(&line, &len, fp)))//读取行信息,'\n'为换行标志

{

pstr = strstr(line, "eth0");//查找改行中是否有"eth0"的字符串

if(NULL != pstr)

{

//printf("%s\n", pstr);

sscanf(pstr, "%s\t%s\t%s", buf1, buf2, buf3);

printf("buf1:%s  buf2:%s  buf3:%s\n", buf1, buf2, buf3);

break;

}

}

//确保空间的释放

if(line)

{

free(line);

}

fclose(fp);

return 0;

}

[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

DESCRIPTION

getline()  reads  an entire line from stream, storing the address of the buffer containing the text into *lineptr.  The buffer is null-

terminated and includes the newline character, if one was found.

If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should be freed by the user  program.   Alterna-

tively,  before calling getline(), *lineptr can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer is not

large enough to hold the line, getline() resizes it with realloc(), updating *lineptr and *n as necessary. In either case,  on  a  suc-

cessful call, *lineptr and *n will be updated to reflect the buffer address and allocated size respectively.

getdelim()  works  like  getline(), except a line delimiter other than newline can be specified as the delimiter argument. As with get-

line(), a delimiter character is not added if one was not present in the input before end of file was reached.

RETURN VALUE

On success, getline() and getdelim() return the number of characters read, including the delimiter character,  but  not  including  the

terminating null byte. This value can be used to handle embedded null bytes in the line read.

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

ERRORS

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

EXAMPLE

#define _GNU_SOURCE

#include

#include

int main(void)

{

FILE * fp;

char * line = NULL;

size_t len = 0;

ssize_t read;

fp = fopen("/etc/motd", "r");

if (fp == NULL)

exit(EXIT_FAILURE);

while ((read = getline(&line, &len, fp)) != -1) {

printf("Retrieved line of length %zu :\n", read);

printf("%s", line);

}

if (line)

free(line);

return EXIT_SUCCESS;

}

CONFORMING TO

Both getline() and getdelim() are GNU extensions.  They are available since libc 4.6.27.

以上就是本文关于C语言中getline()函数的深入理解,希望对大家有所帮助,如有不足之处,请留言,小编会及时更正。感谢朋友们对爱站技术频道的支持!

linux c语言 getline,C语言中getline()函数的深入理解相关推荐

  1. x3用c语言函数表示,C语言中strtod()函数的用法详解

    函数原型: #include double strtod(const char *nptr, char **endptr); C语言及C++中的重要函数. 名称含义 strtod(将字符串转换成浮点数 ...

  2. exit在c语言里的作用,C语言中exit函数的使用

    exit() 结束当前进程/当前程序/,在整个程序中,只要调用 exit ,就结束 return() 是当前函数返回,当然如果是在主函数main, 自然也就结束当前进程了,如果不是,那就是退回上一层调 ...

  3. php seekdir,C++_详解C语言中telldir()函数和seekdir()函数的用法,C语言telldir()函数:取得目录流 - phpStudy...

    详解C语言中telldir()函数和seekdir()函数的用法 C语言telldir()函数:取得目录流的读取位置头文件: #include 定义函数: off_t telldir(DIR *dir ...

  4. _nop_在c语言里什么作用,单片机c语言中nop函数的使用方法和延时计算

    原标题:单片机c语言中nop函数的使用方法和延时计算 标准的C语言中没有空语句.但在 的C语言编程中,经常需要用几个空指令产生短延时的效果. 这在汇编语言中很容易实现,写几个nop就行了. 在C51中 ...

  5. C语言一定要有函数声明吗,1 什么是C语言的隐式函数声明在C语言中,函数在调用前不一定非要声明。如果没有声明,那么编译器会自动按照一种隐式声明的规则,为调用函数的C代码产生汇编代码。下...

    1 什么是C语言的隐式函数声明 在C语言中,函数在调用前不一定非要声明.如果没有声明,那么编译器会自动按照一种隐式声明的规则,为调用函数的C代码产生汇编代码.下面是一个例子: int main(int ...

  6. c语言rand函数的作用,详解C语言中rand函数的使用

    前言 我们在编程实现算法的过程中,往往需要使用到随机数.由于计算机是一台以逻辑为基础的机器,没法做到真正的随机(大概量子计算机可以?).所以计算机生成的是伪随机数,供我们使用. 我们使用C语言的ran ...

  7. c语言memcopy_C语言中memcpy 函数的用法详解

    C语言中memcpy 函数的用法详解 memcpy(内存拷贝函数) c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址 ...

  8. c语言中 调用函数除函数名外,【单选题】在 C 语言中 , 调用函数除函数名外 , 还必须有 ( ). (10.0分) A. 函数预说明 B. 实际参数 C. ( ) D. 函数返回值...

    [单选题]在 C 语言中 , 调用函数除函数名外 , 还必须有 ( ). (10.0分) A. 函数预说明 B. 实际参数 C. ( ) D. 函数返回值 更多相关问题 下列关于IMOECDIS性能标 ...

  9. c语言if函数括号内大于零,c语言中if函数后面的小括号内能写2个判断条件吗?...

    c语言中if函数后面的小括号内可以写2个判断条件: if(a==b||b==c): if(a==d&&b==c): if判断语句的作用:就是当满足一定条件时才会执行那块代码,否则就不执 ...

最新文章

  1. ehcache 简介
  2. 自定义PointViewPager依赖库
  3. win7休眠设置在哪里_电脑休眠好不好?
  4. 封杀所有Bytespider蜘蛛,太频繁,太操蛋,不杀不行~~~
  5. 地震也能照常运行的数据中心新设计
  6. css3常用方法以及css3选择器
  7. 一图看懂圆柱侧面与螺旋线关系,你懂了吗?
  8. Zookeeper常用命令行及API
  9. python中的order_Hive中Order by和Sort by的区别是什么?
  10. 【算法学习】图相关算法编程实现-深度优先遍历和广度优先遍历
  11. 区间DP之环形石子合并
  12. Java集合(一) —— ArrayList
  13. 图像处理之基础---大话小波和卷积
  14. 我觉得idea最好看的编程字体:JetBrains Mono
  15. 就 计算机 而言,为什么大多数情况下非科班生 比不上 科班生
  16. 图论——最短路径之渡河问题
  17. 无法完成操作,因为文件包含病毒或潜在的垃圾软件解决方案
  18. 云服务器到底是什么?云服务器的优势有哪些
  19. 零经验产品经理,思维导图带你从入门到精通成为
  20. 管理创新——从三个和尚的故事说起

热门文章

  1. 【网络信息安全】网络安全与信息安全的关系
  2. sdjzu 1012
  3. ES6读书笔记--一入解构深似海
  4. ES6读书笔记--对js对象爱的深沉
  5. 飞瞳引擎™集装箱人工智能AI识别检测云服务,集装箱人工智能集装箱残损检测信息识别,全球港航人工智能/集装箱人工智能领军者中集飞瞳
  6. Java面试中常见的高并发解决方案
  7. python:密码合格验证程序
  8. Flannel网络配置
  9. BMW 汽车行业E公司 EDI项目案例
  10. 阿里软件测试二面:adb 连接 Android 手机的两种方式,看完你就懂了