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?
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.
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.
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.
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.
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.)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...
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.*
You need to use the return value of getline
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With