Deep understanding of _c language based on getline () function

Source: Internet
Author: User

I searched the internet for half a day getline () function, mostly for C + +, overloaded functions more, foggy, and there is no instance, anyway is not the need for their own getline () function. So, I got a man under Linux and tested it. The function of the getline () function is to get the line information from the file, that is, to read one row at a time.

Because I use the getline () function is to obtain the local network card information, that is, eth0 information, to determine whether the starting machine checked the network cable (could have been done from the drive, but the application layer can be done, do not want to do more processing, understanding).

//function prototypes
#define _gnu_source
#include <stdio.h>
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 2300 0 0 0 0 0 5370 33 0 0 0 0 0-0
[Root@localhost for_test]# Cat Eth0_dev.c

Copy Code code as follows:

#include <stdio.h>
#include <string.h>
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))//Read line information, ' \ n ' is a newline flag
{
PSTR = Strstr (line, "eth0");//Find a string with "eth0" in the row
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
}
}
Ensure the release of space
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]# Mans Getline
Copy Code code as follows:

DESCRIPTION
Getline () reads a 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 () would allocate a buffer for storing the line, which should to freed by the user Progra M. 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, getline () resizes it with realloc (), updating *lineptr and *n as necessary. In either case, on a suc-
Cessful call, *lineptr and *n is 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. As with get-
Line (), a delimiter character is isn't added if one is not present into 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 I Ncluding the
Terminating null byte. This value can is used to handle embedded null bytes into the line read.
Both functions return-1 On Failure to read a (including end of file condition).
ERRORS
Einval bad parameters (n or lineptr are NULL, or stream is not valid).
EXAMPLE
#define _gnu_source
#include <stdio.h>
#include <stdlib.h>
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.