Thread: Getline help

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    16

    Getline help

    I was wondering if someone could tell me what is wrong with this code and what I could do to fix it. It says Segmentation Fault when I run it.

    char * getline(char * string,int max_chars)
    {
    char response;
    char *pstring;
    *pstring = string;

    if (max_chars)
    {
    int number;
    while((response = getchar()) != '\n')
    {
    if (number < max_chars)
    {
    *pstring++ = response;
    number++;
    }
    else
    {
    break;
    }
    }
    }
    else
    {
    while ((responce = getchar()) != 'n')
    {
    *pstring++ = response;
    }
    }
    *pstring = '\0';
    return string;
    }

    int main(void)
    {
    char ff[10];
    getline(ff,30);
    puts(ff);
    return 0;
    }
    Last edited by pr0ficient; 05-10-2002 at 03:08 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char * getline(char * string,int max_chars) 
    { 
        char response; 
        char *pstring; 
        *pstring = string;
    Should be:

    pstring = string;

    Code:
       char ff[10]; 
       getline(ff,30);
    Ack! What are you doing? Can you say "Buffer Overflow"? I thought you could!

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > getline(ff,30);
    Not lying about the size of your buffer would be a start
    Try
    getline(ff,10);

    or
    getline(ff,sizeof(ff));

    http://www.cprogramming.com/cboard/m...bbcode#buttons

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    > getline(ff,30);
    Not lying about the size of your buffer would be a start
    Try
    getline(ff,10);

    or
    getline(ff,sizeof(ff));

    http://www.cprogramming.com/cboard/m...bbcode#buttons
    Fah. You beat me to it. ('m on a phone call) I tend to post and edit and post... as I read down the post at times...

    Quzah.
    Last edited by quzah; 05-10-2002 at 03:26 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if (number < max_chars)
    number is uninitialized here.

    >while ((responce = getchar()) != 'n')
    Spelling. It's response and '\n'.

    -Prelude
    My best code is written with the delete key.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could just save yourself the trouble...

    char * getline(char * string,int max_chars) { fgets( string, max_chars, stdin ); return string; }

    Here's a thought: Why are you bothering to return anything?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    16

    Still Not Working Fully

    The code as of now is:

    #include <stdio.h>

    char * getstring(char * string,int max_chars)
    {
    char responce;
    char * pstring;
    pstring = string;
    int number;

    while ((responce = getchar()) != '\n')
    {
    if (number < max_chars)
    {
    *pstring++ = responce;
    number++;
    continue;
    }
    else
    {
    break;
    }
    }
    *pstring = '\0';
    return string;
    }

    int main(void)
    {
    char ff[10];
    getstring(ff,10);
    puts(ff);
    return 0;
    }


    ..this doesn't work, although it DOES work if i make it:

    char ff[30];
    getstring(ff,30);

    I want this to be able to ask for input then, once it gets a \n or after it get's to max_chars it stops allowing input and then puts() it. Any idea what the trouble is now?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char * pstring;
    pstring = string;
    int number;
    Well, for one, that is not valid C code. It is valid in C++, but not in C (unless it's in the new standard). In C, you must declare all varaibles at the beginning of the scope, then you can make assignments.

    I want this to be able to ask for input then, once it gets a \n or after it get's to max_chars it stops allowing input and then puts() it. Any idea what the trouble is now?
    That depends on what you mean "ask for input then, once it gets... ...it stops allowing input". Do you mean stops reading from the keyboard? Or do you mean it's going to just ignore everything from that point on?

    There is no ANSI standard for just having the program stop reading input from the keyboard. Ie: as soon as I press the key, it ignores them all if I'm past X number of characters.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    16

    Okay

    Say I do getstring(ff,10). I want getstring to read all input until 'number' gets to 10 or if the enter key is pressed before that.

    Example of what I would like it to do:

    I run the function:
    getstring(ff,10);

    I Input:
    abcdefghijklmnop <then press the enter key>

    The program outputs the first 10 characters:
    abcdefghij
    Last edited by pr0ficient; 05-10-2002 at 04:45 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So is there a reason you need to write your own functin? Why not just use fgets?

    fgets( ff, 10, stdin );

    Otherwise:
    Code:
    while( number < max_number-1 )
    {
        string[number] = getch( );
        if( string[number] == '\n )
            break;
    }
    string[number] = '\0';
    Remember: You must add a null. As such, you can only actually read nine characters if you pass it 10.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Have you now added a line like
    >number = 0;
    in the getstring function? In your previous posts, number was not set before it was used.

    Also, the way your code works (last posting of), you exceed the buffer length if the string is longer than max_chars. This is not done with user input, but by the line
    >*pstring = '\0';
    This is because you took a character from the user and put it into the last char of the buffer, then incremented pstring. The next iteration of the read loop caused the break because you had read enough input, but you then went and plonked a \0 into *pstring, which is of course now pointing to one byte past the end of the array. <phew>

    Post your lasted version of code if you still have problems.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    16
    I thought that if you assigned an int within a function it is automatically set to 0.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by pr0ficient
    I thought that if you assigned an int within a function it is automatically set to 0.
    Nope. The only variables that are automaticly initalized are 'static' variables, and then even with those, it's only at the time of creation, not every time the function is called.

    Without you actually initializing a variable, it just hold whatever random value that bit of memory held before it was set aside for that variable.

    For example:

    Code:
    #include <stdio.h>
    int myfun1( void ) { int x; return x; }
    int myfun2( void ) { int x; return x; }
    int myfun3( void ) { int x; return x; }
    int myfun4( void ) { int x; return x; }
    int mystaticfun( void ) { int x; return x; }
    
    int main( void )
    {
        return printf("%d %d %d %d, and static: %d\n",
            myfun1(), myfun2(), myfun3(), myfun4(), mystaticfun() );
    }
    If any of the first 4 are zero, it's only luck or your specific compiler.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM