基于getline()函数的深入理解
本文导语: 我在网上搜了半天getline()函数,大多针对C++的,重载函数比较多,云里雾里的,而且没有实例,反正就是没有自己所需要的getline()函数。所以,自己在Linux下man了一把,并做了测试。getline()函数的功能是从文件中获取行信息,...
我在网上搜了半天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("%sn", pstr);
sscanf(pstr, "%st%st%s", buf1, buf2, buf3);
printf("buf1:%s buf2:%s buf3:%sn", 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.