Learn about the getline function

This is a discussion on Learn about the getline function within the C Programming forums, part of the General Programming Boards category; Hopefully someone can explain to me how the getline function works. Thank you Code: #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  1. #1
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108

    Learn about the getline function

    Hopefully someone can explain to me how the getline function works. Thank you
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
        //welcome to main!
        //we need our input values!
        
        char inputstr[128];
        
        while(1)
        {
                if(feof(stdin) != 0)
                { break; }
                
    getline(&inputstr, 128, NULL);
    getline gets a line in a data file. I assume that inputstr[128] is the name of the file? why is the number of the array in the getline function

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    The getline function is non-standard. However, if you are using a *nix system, running man getline will probably give you the information you want.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    You need to learn C first. None of the arguments you passed to getline are correct.

    EDIT: At the very least, get a good grasp on types as well as a basic understanding of what a stream is.
    Last edited by Barney McGrew; 05-15-2013 at 02:09 AM.

  4. #4
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by Barney McGrew View Post
    You need to learn C first. None of the arguments you passed to getline are correct.

    EDIT: At the very least, get a good grasp on types as well as a basic understanding of what a stream is.
    It isn't my code. I found it in another tutorial website. I am showing it because the tutor said that getline would be a lot better to use. My code involves using fscanf and fprintf

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    I assumed that you were asking how to use getline, i.e., to get it to work for you. This is why I suggested that you read the relevant manual entry. That has not changed: you need to learn the declaration of the function and read an explanation of its parameters and return value, plus possible catches (e.g., the need to call free), possibly with examples.

    Barney McGrew's response targets the question of how the getline function works, as in how might it be implemented and why. You are obviously not ready for that yet, and it was not your intended question anyway.

    Quote Originally Posted by arti
    My code involves using fscanf
    In that case, your code may change to use getline in conjunction with sscanf instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Barney McGrew's response targets the question of how the getline function works, as in how might it be implemented and why.
    I was responding to how they're using it, since according to my man-page the prototype is as follows:

    Code:
    ssize_t getline(char **lineptr, size_t *n, FILE *stream);
    Going by the way it's been called in the code posted by arti, they convert a 'char (*)[128]' to a 'char **' and an 'int' to a 'size_t *'. They also pass a null pointer to the third parameter, which is a valid conversion but an invalid value for the function. An understanding of types and streams would, ideally, help them understand these problems.

  7. #7
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by laserlight View Post
    I assumed that you were asking how to use getline, i.e., to get it to work for you. This is why I suggested that you read the relevant manual entry. That has not changed: you need to learn the declaration of the function and read an explanation of its parameters and return value, plus possible catches (e.g., the need to call free), possibly with examples.

    Barney McGrew's response targets the question of how the getline function works, as in how might it be implemented and why. You are obviously not ready for that yet, and it was not your intended question anyway.


    In that case, your code may change to use getline in conjunction with sscanf instead.
    I did read it laserlight, but it wouldn't work in my program even though I followed the example in the man getline of linux. Which is when i switched to fgets because the tutor and another student said that fgets is simpler and works.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by arti View Post
    I did read it laserlight, but it wouldn't work in my program even though I followed the example in the man getline of linux. Which is when i switched to fgets because the tutor and another student said that fgets is simpler and works.
    The question is, do you know the maximum input line you are expecting? If than answer is yes, how much does it matter in the great scheme of things if user feeds you an over-long line?
    If tha answer to question 1 is "no", don't use fgets(). If the answer to question 1 is "yes" and the answer to question 2 is "not much", just call fgets(). If the answer to question 2 is "it matters that long lines be handled correctly", use getline(). If the answer to question 2 is "it is absolutely imperative that the program behave correctly, under all inputs", don't use getline(). Use fgets() then consume the buffer if you don't read the trailing newline, then print out some sort of error message.
    The reason is that getline() is potentially vulnerable to a resource denial attack.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with getline function
    By chickenlittle in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2010, 02:04 PM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. stuck with getline() function
    By stalker in forum C++ Programming
    Replies: 15
    Last Post: 10-24-2003, 08:42 AM
  4. getline() function
    By unanimous in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2001, 08:58 PM
  5. getline function
    By JamMan in forum C++ Programming
    Replies: 11
    Last Post: 11-25-2001, 05:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21