What is the difference between gets and getline function




















Thank you for your answer, I have another question regarding to this matter. What if the input characters exceed the character limit for both of these functions, what type of error would it possibly caused? Pavel Smirnov 4, 3 3 gold badges 15 15 silver badges 25 25 bronze badges. Really nice explanation. The flipping of the code lines to see the different results is an unforgettable example!

Learnt a lot today thanks to you! Shubham Aggarwal Shubham Aggarwal 5 5 bronze badges. Ashu Ashu 11 1 1 bronze badge. Omar Ashraf Omar Ashraf 1. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? It stores the string in the string variable passed to it, adding a null character to terminate the string. This function takes three parameters: the first is the string into which to read data, the second is the maximum number of characters to read. The third parameter is the stream from which to read. The number of characters that fgets reads is one less than than number specified; it stores the null character in the last character space.

You must supply at least as many characters of space in the string, or your program might crash, but at least the fgets function protects against overflowing the string and creating a security hazard. If there is no error, fgets returns the string read as a return value else for example if the stream is already at end of file, it returns a null pointer.

Unfortunately, like the gets function, fgets is deprecated, in this case because when fgets cannot tell whether a null character is included in the string it reads. If a null character is read by fgets, it will be stored in the string along with the rest of the characters read. Since a null character terminates a string in C, C will end your string prematurely, right before the first null character. Only use fgets if you are certain the data read cannot contain a null; otherwise, use getline.

Using getline function in C: Preferred Method : The getline function is the preferred method for reading lines of text. The other functions like gets, fgets, and scanf, are unreliable for reasons already seen above and must be avoided. The getline function reads an entire line from a stream, up to and including the next newline character. The first is a pointer to a block allocated with malloc or calloc which allocates memory for the program when run. The third parameter is simply the stream from where to read the line.

The getline function automatically will enlarge the block of memory as needed, via realloc function, so there is never a shortage of space one reason why getline is safe. We will understand better in the below mentioned example. It will also tell us the new size of the block by the value returned in the second parameter.

If an error occurs, such as end of file etc. Although the second parameter is of type pointer to string , you cannot treat it as an ordinary string, since it may contain null characters before the final null character marking the end of the line. The return value enables you to distinguish null characters that getline read as part of the line, by specifying the size of the line.

Here is an example of how to use getline to read a line of text from the standard input. How have you been? You entered the following string: Hello! Current size for string block: 27 Here we have typed more than 10 characters. However, getline still safely handled the input and read the data entirely as it dynamically reallocates space to accomodate the string. Using getdelim function: Preferred Method : The getdelim function is a more general form.

In fact, getline simply calls getdelim and specifies that the delimiter character is a newline. The syntax for getdelim is nearly the same as that of getline, except that the third parameter is the delimiter character, and the fourth parameter is the stream from which to read.

In order to read a string, we have to use this function repeatedly until a terminating character is encountered. The characters scanned one after the other have to be stored simultaneously into the character array. Entered string is: Where have you been? Hence condition is checked against it. This function prints a text string to the terminal i.

It is one of the preferred modes to output a string. So what I don't understand now is shouldn't that newline character be placed into name when it calls getname a second time? Therefore the cin. What is the flaw to my logic? Basically, the difference between get and getline when you're reading C strings is that get leaves the delimiting character a newline character, in this case in the input buffer as the next character to be read, while getline actually extracts and discards the delimiting character.

So the reason the code doesn't work when you use get is that after the first time you call cin. Then, the second time you call it, cin immediately runs into the delimiting character which is still in the input buffer and thinks that the input stops right there, so it doesn't read any characters. It works with getline since getline gets rid of that delimiter after it reads the input.



0コメント

  • 1000 / 1000