Getline () function

Source: Internet
Author: User

I searched the internet for the getline () function for a long time, mostly for C ++. There are many heavy-duty functions in the cloud, and there are no instances. Actually, there is no getline () required by myself () function. Therefore, I ran man in Linux and tested it. The getline () function obtains row information from a file, that is, each row is read.

Because the purpose of using the getline () function is to obtain the information of the local network adapter, that is, the eth0, so as to determine whether the network cable is checked during the start of the driver (which can be done from the driver, but the application layer can handle it, so I don't want to do more. Sorry ).

 

// Function prototype

# 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 230 0 0 0 0 0 5370 33 0 0 0 0 0 0

 

 

[Root @ localhost for_test] # cat eth0_dev.c

[Cpp]
# 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 the row information. '\ n' indicates the line feed.
{
Pstr = strstr (line, "eth0"); // you can check whether the "eth0" string exists in the modified line.
If (NULL! = Pstr)
{
// Printf ("% s \ n", pstr );
Sscanf (pstr, "% s \ t % s", buf1, buf2, buf3 );
Printf ("buf1: % s buf2: % s buf3: % s \ n", buf1, buf2, buf3 );
Break;
}
}
// Ensure the space is released
If (line)
{
Free (line );
}
Fclose (fp );
Return 0;
}

# 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 the row information. '\ n' indicates the line feed.
{
Pstr = strstr (line, "eth0"); // you can check whether the "eth0" string exists in the modified line.
If (NULL! = Pstr)
{
// Printf ("% s \ n", pstr );
Sscanf (pstr, "% s \ t % s", buf1, buf2, buf3 );
Printf ("buf1: % s buf2: % s buf3: % s \ n", buf1, buf2, buf3 );
Break;
}
}
// Ensure the space is released
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

[Cpp]
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 provided des the newline character, if one was found.
 
If * lineptr is NULL, then getline () will allocate a buffer for storing the line, which shoshould 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 (), cannot t 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
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 <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
Both getline () and getdelim () are GNU extensions. They are available since libc 4.6.27.

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 provided des the newline character, if one was found.

If * lineptr is NULL, then getline () will allocate a buffer for storing the line, which shoshould 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 (), cannot t 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
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 <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
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.