entering string variable size

This is a discussion on entering string variable size within the C Programming forums, part of the General Programming Boards category; I'm making program that asks user to enter some string. I use char *string; gets(string); I' m wondering if this ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    entering string variable size

    I'm making program that asks user to enter some string. I use char *string;
    gets(string);
    I' m wondering if this is a good way to get string from user and if there is possibility that user enter string so big to overwrite some other later data in my program and cause run-time error, because string is pointer to first byte of data.
    one of the possible solution is
    char string[80];
    this will force compiler to set aside memory but then I'll have fixed size of string.
    Thank you

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    char * fgets(char * buffer, int max_len, FILE * stream);

    For console input, the 'stream' would be 'stdin'.

    [edit]
    Just reread your post...
    make sure the pointer you are using points to valid memory!
    [/edit]
    Last edited by Sebastiani; 11-22-2003 at 02:58 PM.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I' m wondering if this is a good way to get string from user
    Not at all, string points to an indeterminate address and gets doesn't restrict the user from entering more data than you can handle. For reading a line, use fgets.

    >but then I'll have fixed size of string
    You have two options: Use a fixed length array as the upper limit of the string size, or use malloc and friends to maintain an arbitrary length string. The former is simpler, the latter is more flexible.
    My best code is written with the delete key.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    using malloc

    You suggesst me to use malloc. But when I call malloc I must specify how much room I want to set aside

    void *malloc( size_t size );

    How can I know size when expecting user to enter string from keyboard?
    size=sizeof(char)*strlen(string)+1.
    This means that I already store string, so upper way is useless.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    > How can I know size when expecting user to enter string from keyboard?
    You can't.

    You always have to read from stdin using fgets() into some fixed sized buffer. If the user decides to type in a lot, then you can start calling memory allocation routines to save each fragment in say a linked list.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
        char *p = strchr( buff, '\n' );  /* find the newline */
        if ( p == NULL ) {
            /* no newline, user filled the buffer */
            /* copy this buff to allocated memory and */
            /* repeat calls to fgets() */
        } else {
            /* do something with buff */
            /* and/or the allocated memory containing partial input */
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156

    Re: entering string variable size

    Originally posted by Micko
    I'm making program that asks user to enter some string. I use char *string;
    gets(string);
    I' m wondering if this is a good way to get string from user and if there is possibility that user enter string so big to overwrite some other later data in my program and cause run-time error, because string is pointer to first byte of data.
    one of the possible solution is
    char string[80];
    this will force compiler to set aside memory but then I'll have fixed size of string.
    Thank you
    Forget the malloc
    Forget the linked list
    The only way to read in a string at your level of knowledge is to use fgets() as suggested and specify a char string[MX];
    where MX is the maximum size of your potential input. If you're worried about someone typing too much, make MX = 2048 -- there ain't nobody gonna type that many characters from the keyboard.

    All normal string input must go into a pre-defined buffer unless you are at a level of skill to deal with dynamic buffers, direct character input, etc. Learn the basics first.
    Last edited by WaltP; 11-23-2003 at 12:12 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    You always have to read from stdin using fgets() into some fixed sized buffer. If the user decides to type in a lot, then you can start calling memory allocation routines to save each fragment in say a linked list.
    fgetc doesn't exist any more? No one said it had to be efficient.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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