Thread: fgets & storing

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    fgets & storing

    hi all , i just have a problem concerning fgets

    if i use fgets in a linked list structure

    fgets (element->name,MAX,stdin);

    where "name" is an element of struct~


    how can i store the input from stdin , without , storing the newline character into the linked list??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > how can i store the input from stdin , without , storing the newline character into the linked list??
    You can't - fgets comes with a newline appended.

    So you have to get rid of it after calling fgets

    Here's an easy way
    Code:
    char *p = strchr( element->name, '\n' );
    if ( p != NULL ) *p = '\0';
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM