Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of line in getline() function - c

Tags:

c

I have a question regarding the function getline():

getline(&line, &len, file_in);

where file_in is:

FILE *file_in;

char *line = NULL;

size_t len;

Every time when I read any line the allocated memory (len variable) is 120

I suppose that it is constant, because it is a default line size in a file.

1) But why 120?

2) Can this default size be changed anywhere?

3) Is there any C function that counts only written chars?

like image 986
JosephConrad Avatar asked Nov 17 '13 20:11

JosephConrad


People also ask

How does getline () work in C?

The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.

Does C Getline include newline?

If successful, getline() returns the number of characters that are read, including the newline character, but not including the terminating null byte ( '\0' ). This value can be used to handle embedded null bytes in the line read.

Does Getline work with C strings?

Reading strings: get and getlineThe functions get and getline (with the three parameters) will read and store a c-style string. The parameters: First parameter (str) is the char array where the data will be stored.

What are the parameters for Getline?

The second method of declaring the C++ getline() function with two parameters is: istream& getline( istream& is, string& str ); In the above syntax, istream& getline is to define the function, and the three parameters are: istream& is: This is an istream class's object to specify the location to read the input stream.


3 Answers

  1. Using 120 as an initial allocation is a sensible choice made by those who wrote the library; it is longer than most lines, so there's a decent chance it will only have to do one memory allocation. (FWIW: I wrote an implementation of getline() and getdelim() to use on machines without a version in the standard library. It used 256 as the minimum allocation (rather than 120) — except when compiled for testing, when it used 8 as the starting point to make sure the reallocation code gets more exercise. The allocated size doubles when there isn't enough room. It was a similar thought process to what the library designers did in the library you use; get the memory allocation big enough first time around that the code mostly won't need to reallocate.)
  2. You can find the code in the library, change the default, and rebuild the library. OTOH, that's hard work.
  3. There's a function called getline() that tells you how many chars were in the line it just read via its return value. Note that it returns -1 (not EOF) on error. Although it seldom happens, it is theoretically possible for EOF to be a negative value other than -1.

You can also rig the system to use shorter lines (until you read a long line) by:

FILE *file_in;
size_t len = 16;
char *line = malloc(len);

And (for the ultimate in sleazeball programming) you don't even have to error check the allocation since char *line = NULL; is also a valid input to getline():

ssize_t nbytes;

while ((nbytes = getline(&line, &len, file_in)) > 0)
    ...process a line of nbytes including newline...
like image 66
Jonathan Leffler Avatar answered Oct 07 '22 11:10

Jonathan Leffler


getline() will extend len and realloc line as need be to fit whatever input is read.

You probably have not seen a line > 120 yet.

To get the length of the line read, you can use the return value or strlen : int num_chars = strlen(line);

The easiest way to use getline is like this:

size_t len = 0;
char *line = 0;

while(...) {
    size_t n = get_line(&line, &len, file);
    // n is the number of chars in line, len is the total size of the line buffer.
}

getline() will change line and len as needed.

*Most important of all, read the getline manpage.*

like image 41
Charlie Burns Avatar answered Oct 07 '22 12:10

Charlie Burns


You need to use the return value of getline.

like image 1
R.. GitHub STOP HELPING ICE Avatar answered Oct 07 '22 11:10

R.. GitHub STOP HELPING ICE